Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions apps/launcher/launcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@
#define HOME_BUTTON_HEIGHT 48
#define HOME_BUTTON_GAP 16

#define HOME_HOVER_COLOR ((gfx_color_t){0x28, 0x28, 0x28})

#define TOOLTIP_MAX_CHARS 16
#define TOOLTIP_PAD_X 8
#define TOOLTIP_PAD_Y 4
#define TOOLTIP_GAP 6
#define TOOLTIP_MAX_W (TOOLTIP_MAX_CHARS * FONT_WIDTH + 2 * TOOLTIP_PAD_X)
#define TOOLTIP_H (FONT_HEIGHT + 2 * TOOLTIP_PAD_Y)
#define TOOLTIP_COLOR ((gfx_color_t){0x30, 0x30, 0x30})

// --- apps ---

static void start_terminal(void) {
Expand Down Expand Up @@ -89,6 +99,21 @@ static const app_t* volatile running_app = 0;

static int taskbar_slot_app[MAX_APPS];

// --- hover state ---

static int hover_tile = -1;
static int hover_slot = -1;
static title_bar_hit_t hover_title = TITLE_BAR_HIT_NONE;

static int32_t last_mouse_x = -1;
static int32_t last_mouse_y = -1;

static gfx_color_t tooltip_saved[TOOLTIP_H][TOOLTIP_MAX_W];
static uint32_t tooltip_x = 0;
static uint32_t tooltip_y = 0;
static uint32_t tooltip_w = 0;
static int tooltip_up = 0;

// --- helpers ---

static int app_index(const app_t* app) {
Expand Down Expand Up @@ -178,6 +203,9 @@ static void draw_home_tile(int index) {
const button_t* tile = &home_buttons[index];
const char* name = apps[index].name;

gfx_color_t background = (index == hover_tile) ? HOME_HOVER_COLOR : GFX_BLACK;
gfx_fill_rect(tile->x, tile->y, tile->width, tile->height, background);

uint32_t icon_x = tile->x + (tile->width - HOME_ICON_PX) / 2;
uint32_t icon_y = tile->y + HOME_TILE_PAD;

Expand Down Expand Up @@ -221,19 +249,156 @@ static void draw_home_screen(void) {
draw_centered_string(gfx_screen_height() - HOME_HINT_MARGIN, HOME_HINT, GFX_WHITE);
}

// --- tooltip ---

static void tooltip_show(int slot) {
uint32_t screen_w = gfx_screen_width();
uint32_t screen_h = gfx_screen_height();

if (screen_h < TASKBAR_HEIGHT + TOOLTIP_GAP + TOOLTIP_H) {
return;
}

const char* name = apps[taskbar_slot_app[slot]].name;
char text[TOOLTIP_MAX_CHARS + 1];
uint32_t chars = 0;
while (name[chars] != '\0' && chars < TOOLTIP_MAX_CHARS) {
text[chars] = name[chars];
chars++;
}
text[chars] = '\0';

uint32_t width = chars * FONT_WIDTH + 2 * TOOLTIP_PAD_X;
uint32_t center = taskbar_slot_center_x(slot);

uint32_t x = 0;
if (center > width / 2) {
x = center - width / 2;
}
if (x + width > screen_w) {
x = (screen_w > width) ? screen_w - width : 0;
}

tooltip_x = x;
tooltip_y = screen_h - TASKBAR_HEIGHT - TOOLTIP_GAP - TOOLTIP_H;
tooltip_w = width;

mouse_hide_cursor();

for (uint32_t row = 0; row < TOOLTIP_H; row++) {
for (uint32_t col = 0; col < tooltip_w; col++) {
tooltip_saved[row][col] = gfx_get_pixel(tooltip_x + col, tooltip_y + row);
}
}

gfx_fill_rect(tooltip_x, tooltip_y, tooltip_w, TOOLTIP_H, TOOLTIP_COLOR);
gfx_draw_rect_outline(tooltip_x, tooltip_y, tooltip_w, TOOLTIP_H, GFX_WHITE);
gfx_draw_string(tooltip_x + TOOLTIP_PAD_X, tooltip_y + TOOLTIP_PAD_Y, text, GFX_WHITE);

tooltip_up = 1;
}

static void tooltip_hide(void) {
if (!tooltip_up) {
return;
}

mouse_hide_cursor();

for (uint32_t row = 0; row < TOOLTIP_H; row++) {
for (uint32_t col = 0; col < tooltip_w; col++) {
gfx_put_pixel(tooltip_x + col, tooltip_y + row, tooltip_saved[row][col]);
}
}

tooltip_up = 0;
}

// drops the tooltip without restoring, for when the screen is about to be repainted anyway
static void tooltip_forget(void) {
tooltip_up = 0;
}

// --- hover ---

static void reset_hover(void) {
hover_tile = -1;
hover_slot = -1;
hover_title = TITLE_BAR_HIT_NONE;

taskbar_set_hover(-1);
title_bar_set_hover(TITLE_BAR_HIT_NONE);
tooltip_forget();
}

static void update_hover(int32_t mouse_x, int32_t mouse_y) {
if (mouse_x == last_mouse_x && mouse_y == last_mouse_y) {
return;
}
last_mouse_x = mouse_x;
last_mouse_y = mouse_y;

const app_t* app = running_app;

int tile = -1;
title_bar_hit_t title = TITLE_BAR_HIT_NONE;
if (app == 0) {
tile = button_find_at(home_buttons, app_count, mouse_x, mouse_y);
} else {
title = title_bar_hit_test(mouse_x, mouse_y);
}
int slot = taskbar_hit_test(mouse_x, mouse_y);

if (tile != hover_tile) {
int old_tile = hover_tile;
hover_tile = tile;

mouse_hide_cursor();
if (old_tile >= 0) {
draw_home_tile(old_tile);
}
if (tile >= 0) {
draw_home_tile(tile);
}
}

if (app != 0 && title != hover_title) {
hover_title = title;
title_bar_set_hover(title);

mouse_hide_cursor();
title_bar_draw(app->name);
}

if (slot != hover_slot) {
hover_slot = slot;
tooltip_hide();
taskbar_set_hover(slot);

mouse_hide_cursor();
draw_taskbar();

if (slot >= 0) {
tooltip_show(slot);
}
}
}

static void show_home_screen(void) {
running_app = 0;
menu_close();
prompt_close();
dialog_close();
mouse_left_clicked();
reset_hover();
draw_home_screen();
}

// --- switching between apps ---

static void launch_app(int index) {
mouse_hide_cursor();
reset_hover();

if (app_state[index] == APP_STATE_MINIMIZED && apps[index].redraw != 0) {
apps[index].redraw();
Expand Down Expand Up @@ -436,6 +601,7 @@ static void handle_dialog_click(int32_t x, int32_t y) {
// --- keyboard ---

static void launcher_handle_key(char c) {
tooltip_hide();
const app_t* app = running_app;

if (app == 0) {
Expand Down Expand Up @@ -524,6 +690,8 @@ void launcher_update(void) {
return;
}

update_hover(mouse_x, mouse_y);

if (clicked) {
int slot = taskbar_hit_test(mouse_x, mouse_y);
if (slot >= 0) {
Expand Down
14 changes: 7 additions & 7 deletions kernel/ui/dialog.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#define MESSAGE_Y_OFFSET 38
#define BUTTON_Y_OFFSET 72

#define BUTTON_W 112
#define BUTTON_H 32
#define BUTTON_GAP 12
#define DIALOG_BUTTON_W 112
#define DIALOG_BUTTON_H 32
#define DIALOG_BUTTON_GAP 12

#define TEXT_MAX 48

Expand Down Expand Up @@ -74,14 +74,14 @@ static void place_box(void) {
}

static void place_buttons(const char* const* labels, int count) {
uint32_t row_width = (uint32_t) count * BUTTON_W + (uint32_t) (count - 1) * BUTTON_GAP;
uint32_t row_width = (uint32_t) count * DIALOG_BUTTON_W + (uint32_t) (count - 1) * DIALOG_BUTTON_GAP;
uint32_t start_x = box_x + (BOX_WIDTH - row_width) / 2;

for (int i = 0; i < count; i++) {
buttons[i].x = start_x + (uint32_t) i * (BUTTON_W + BUTTON_GAP);
buttons[i].x = start_x + (uint32_t) i * (DIALOG_BUTTON_W + DIALOG_BUTTON_GAP);
buttons[i].y = box_y + BUTTON_Y_OFFSET;
buttons[i].width = BUTTON_W;
buttons[i].height = BUTTON_H;
buttons[i].width = DIALOG_BUTTON_W;
buttons[i].height = DIALOG_BUTTON_H;
buttons[i].label = labels[i];
buttons[i].color = BUTTON_COLOR;
buttons[i].on_click = 0;
Expand Down
17 changes: 17 additions & 0 deletions kernel/ui/taskbar.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define FALLBACK_COLOR ((gfx_color_t){0x40, 0x40, 0x40})
#define FOCUS_BAR_COLOR ((gfx_color_t){0x30, 0xd0, 0x30})
#define OPEN_BAR_COLOR ((gfx_color_t){0x90, 0x90, 0x90})
#define HOVER_COLOR ((gfx_color_t){0x30, 0x30, 0x30})

#define ITEM_WIDTH 48
#define ITEM_GAP 4
Expand All @@ -26,6 +27,7 @@

static button_t item_buttons[TASKBAR_MAX_ITEMS];
static int item_count = 0;
static int hovered_slot = -1;

// --- drawing one item ---

Expand Down Expand Up @@ -92,6 +94,10 @@ void taskbar_draw(const taskbar_item_t* items, int count) {
slot->color = TASKBAR_COLOR;
slot->on_click = 0;

if (item_count == hovered_slot) {
gfx_fill_rect(slot->x, slot->y+1, slot->width, slot->height-1, HOVER_COLOR);
}

draw_item(slot, &items[i]);

item_count++;
Expand All @@ -102,3 +108,14 @@ void taskbar_draw(const taskbar_item_t* items, int count) {
int taskbar_hit_test(int32_t px, int32_t py) {
return button_find_at(item_buttons, item_count, px, py);
}

void taskbar_set_hover(int slot) {
hovered_slot = slot;
}

uint32_t taskbar_slot_center_x(int slot) {
if (slot < 0 || slot >= item_count) {
return 0;
}
return item_buttons[slot].x + item_buttons[slot].width / 2;
}
3 changes: 3 additions & 0 deletions kernel/ui/taskbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ typedef struct {
void taskbar_draw(const taskbar_item_t* items, int count);
int taskbar_hit_test(int32_t px, int32_t py);

void taskbar_set_hover(int slot);
uint32_t taskbar_slot_center_x(int slot);

#endif
13 changes: 11 additions & 2 deletions kernel/ui/title_bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@
#define TITLE_TEXT_X 12

#define BAR_BUTTON_WIDTH TITLE_BAR_HEIGHT

#define CLOSE_BUTTON_COLOR ((gfx_color_t){0xa0, 0x20, 0x20})
#define MINIMIZE_BUTTON_COLOR ((gfx_color_t){0x40, 0x40, 0x40})
#define CLOSE_HOVER_COLOR ((gfx_color_t){0xd0, 0x40, 0x40})
#define MINIMIZE_HOVER_COLOR ((gfx_color_t){0x60, 0x60, 0x60})

#define CLOSE_SLOT 0
#define MINIMIZE_SLOT 1

static button_t close_button;
static button_t minimize_button;

static title_bar_hit_t hovered_hit = TITLE_BAR_HIT_NONE;

static void place_button(button_t* button, uint32_t slot, const char* label, gfx_color_t color) {
uint32_t screen_w = gfx_screen_width();
uint32_t needed = (slot + 1) * BAR_BUTTON_WIDTH;
Expand Down Expand Up @@ -47,13 +52,17 @@ void title_bar_draw(const char* title) {
uint32_t text_y = (TITLE_BAR_HEIGHT - FONT_HEIGHT) / 2;
gfx_draw_string(TITLE_TEXT_X, text_y, title, TITLE_TEXT_COLOR);

place_button(&close_button, CLOSE_SLOT, "X", CLOSE_BUTTON_COLOR);
place_button(&minimize_button, MINIMIZE_SLOT, "-", MINIMIZE_BUTTON_COLOR);
place_button(&close_button, CLOSE_SLOT, "X", (hovered_hit == TITLE_BAR_HIT_CLOSE) ? CLOSE_HOVER_COLOR : CLOSE_BUTTON_COLOR);
place_button(&minimize_button, MINIMIZE_SLOT, "-", (hovered_hit == TITLE_BAR_HIT_MINIMIZE) ? MINIMIZE_HOVER_COLOR : MINIMIZE_BUTTON_COLOR);

button_draw(&close_button);
button_draw(&minimize_button);
}

void title_bar_set_hover(title_bar_hit_t hit) {
hovered_hit = hit;
}

title_bar_hit_t title_bar_hit_test(int32_t px, int32_t py) {
if (button_contains(&close_button, px, py)) {
return TITLE_BAR_HIT_CLOSE;
Expand Down
1 change: 1 addition & 0 deletions kernel/ui/title_bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ typedef enum {
} title_bar_hit_t;

void title_bar_draw(const char* title);
void title_bar_set_hover(title_bar_hit_t hit);

title_bar_hit_t title_bar_hit_test(int32_t px, int32_t py);

Expand Down
Loading