Merge menubar into menu
This commit is contained in:
parent
5f96a5e82a
commit
0e45293ced
@ -5,7 +5,6 @@
|
||||
#include <toaru/graphics.h>
|
||||
#include <toaru/decorations.h>
|
||||
#include <toaru/menu.h>
|
||||
#include <toaru/menubar.h>
|
||||
|
||||
#define APPLICATION_TITLE "File Browser"
|
||||
|
||||
|
@ -5,7 +5,6 @@
|
||||
#include <toaru/graphics.h>
|
||||
#include <toaru/decorations.h>
|
||||
#include <toaru/menu.h>
|
||||
#include <toaru/menubar.h>
|
||||
|
||||
#define APPLICATION_TITLE "Help Browser"
|
||||
|
||||
|
@ -44,7 +44,6 @@
|
||||
#include <toaru/spinlock.h>
|
||||
#include <toaru/list.h>
|
||||
#include <toaru/menu.h>
|
||||
#include <toaru/menubar.h>
|
||||
#include <toaru/sdf.h>
|
||||
|
||||
#include "terminal-palette.h"
|
||||
|
@ -83,3 +83,29 @@ extern int menu_definitely_close(struct MenuList * menu);
|
||||
extern struct MenuSet * menu_set_create(void);
|
||||
extern void menu_set_insert(struct MenuSet * set, char * action, struct MenuList * menu);
|
||||
extern void menu_update_title(struct MenuEntry * self, char * new_title);
|
||||
|
||||
#define MENU_BAR_HEIGHT 24
|
||||
|
||||
struct menu_bar_entries {
|
||||
char * title;
|
||||
char * action;
|
||||
};
|
||||
|
||||
struct menu_bar {
|
||||
int x;
|
||||
int y;
|
||||
int width;
|
||||
|
||||
struct menu_bar_entries * entries;
|
||||
|
||||
struct MenuSet * set;
|
||||
|
||||
struct menu_bar_entries * active_entry;
|
||||
struct MenuList * active_menu;
|
||||
int active_menu_wid;
|
||||
|
||||
void (*redraw_callback)(void);
|
||||
};
|
||||
|
||||
extern void menu_bar_render(struct menu_bar * self, gfx_context_t * ctx);
|
||||
extern int menu_bar_mouse_event(yutani_t * yctx, yutani_window_t * window, struct menu_bar * self, struct yutani_msg_window_mouse_event * me, int x, int y);
|
||||
|
@ -1,29 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <toaru/menu.h>
|
||||
|
||||
#define MENU_BAR_HEIGHT 24
|
||||
|
||||
struct menu_bar_entries {
|
||||
char * title;
|
||||
char * action;
|
||||
};
|
||||
|
||||
struct menu_bar {
|
||||
int x;
|
||||
int y;
|
||||
int width;
|
||||
|
||||
struct menu_bar_entries * entries;
|
||||
|
||||
struct MenuSet * set;
|
||||
|
||||
struct menu_bar_entries * active_entry;
|
||||
struct MenuList * active_menu;
|
||||
int active_menu_wid;
|
||||
|
||||
void (*redraw_callback)(void);
|
||||
};
|
||||
|
||||
extern void menu_bar_render(struct menu_bar * self, gfx_context_t * ctx);
|
||||
extern int menu_bar_mouse_event(yutani_t * yctx, yutani_window_t * window, struct menu_bar * self, struct yutani_msg_window_mouse_event * me, int x, int y);
|
69
lib/menu.c
69
lib/menu.c
@ -768,3 +768,72 @@ int menu_process_event(yutani_t * yctx, yutani_msg_t * m) {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void menu_bar_render(struct menu_bar * self, gfx_context_t * ctx) {
|
||||
int _x = self->x;
|
||||
int _y = self->y;
|
||||
int width = self->width;
|
||||
|
||||
uint32_t menu_bar_color = rgb(59,59,59);
|
||||
for (int y = 0; y < MENU_BAR_HEIGHT; ++y) {
|
||||
for (int x = 0; x < width; ++x) {
|
||||
GFX(ctx, x+_x,y+_y) = menu_bar_color;
|
||||
}
|
||||
}
|
||||
|
||||
/* for each menu entry */
|
||||
int offset = _x;
|
||||
struct menu_bar_entries * _entries = self->entries;
|
||||
|
||||
while (_entries->title) {
|
||||
int w = draw_sdf_string_width(_entries->title, 16, SDF_FONT_THIN) + 10;
|
||||
if ((self->active_menu && hashmap_has(menu_get_windows_hash(), (void*)self->active_menu_wid)) && _entries == self->active_entry) {
|
||||
for (int y = _y; y < _y + MENU_BAR_HEIGHT; ++y) {
|
||||
for (int x = offset + 2; x < offset + 2 + w; ++x) {
|
||||
GFX(ctx, x, y) = rgb(93,163,236);
|
||||
}
|
||||
}
|
||||
}
|
||||
offset += draw_sdf_string(ctx, offset + 4, _y + 2, _entries->title, 16, rgb(255,255,255), SDF_FONT_THIN) + 10;
|
||||
_entries++;
|
||||
}
|
||||
}
|
||||
|
||||
void menu_bar_show_menu(yutani_t * yctx, yutani_window_t * window, struct menu_bar * self, int offset, struct menu_bar_entries * _entries) {
|
||||
struct MenuList * new_menu = menu_set_get_menu(self->set, _entries->action);
|
||||
menu_show(new_menu, yctx);
|
||||
yutani_window_move(yctx, new_menu->window, window->x + offset, window->y + self->y + MENU_BAR_HEIGHT);
|
||||
self->active_menu = new_menu;
|
||||
self->active_menu_wid = new_menu->window->wid;
|
||||
self->active_entry = _entries;
|
||||
if (self->redraw_callback) {
|
||||
self->redraw_callback();
|
||||
}
|
||||
}
|
||||
|
||||
int menu_bar_mouse_event(yutani_t * yctx, yutani_window_t * window, struct menu_bar * self, struct yutani_msg_window_mouse_event * me, int x, int y) {
|
||||
if (x < self->x || x >= self->x + self->width || y < self->y || y >= self->y + 24 /* base height */) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int offset = self->x;
|
||||
|
||||
struct menu_bar_entries * _entries = self->entries;
|
||||
|
||||
while (_entries->title) {
|
||||
int w = draw_sdf_string_width(_entries->title, 16, SDF_FONT_THIN) + 10;
|
||||
if (x >= offset && x < offset + w) {
|
||||
if (me->command == YUTANI_MOUSE_EVENT_CLICK || _close_enough(me)) {
|
||||
menu_bar_show_menu(yctx, window, self,offset,_entries);
|
||||
} else if (self->active_menu && hashmap_has(menu_get_windows_hash(), (void*)self->active_menu_wid) && _entries != self->active_entry) {
|
||||
menu_definitely_close(self->active_menu);
|
||||
menu_bar_show_menu(yctx, window, self,offset,_entries);
|
||||
}
|
||||
}
|
||||
|
||||
offset += w;
|
||||
_entries++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,83 +0,0 @@
|
||||
#include <toaru/yutani.h>
|
||||
#include <toaru/graphics.h>
|
||||
#include <toaru/menu.h>
|
||||
#include <toaru/menubar.h>
|
||||
#include <toaru/sdf.h>
|
||||
|
||||
#include <math.h>
|
||||
|
||||
static int _close_enough(struct yutani_msg_window_mouse_event * me) {
|
||||
if (me->command == YUTANI_MOUSE_EVENT_RAISE && sqrt(pow(me->new_x - me->old_x, 2) + pow(me->new_y - me->old_y, 2)) < 10) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void menu_bar_render(struct menu_bar * self, gfx_context_t * ctx) {
|
||||
int _x = self->x;
|
||||
int _y = self->y;
|
||||
int width = self->width;
|
||||
|
||||
uint32_t menu_bar_color = rgb(59,59,59);
|
||||
for (int y = 0; y < MENU_BAR_HEIGHT; ++y) {
|
||||
for (int x = 0; x < width; ++x) {
|
||||
GFX(ctx, x+_x,y+_y) = menu_bar_color;
|
||||
}
|
||||
}
|
||||
|
||||
/* for each menu entry */
|
||||
int offset = _x;
|
||||
struct menu_bar_entries * _entries = self->entries;
|
||||
|
||||
while (_entries->title) {
|
||||
int w = draw_sdf_string_width(_entries->title, 16, SDF_FONT_THIN) + 10;
|
||||
if ((self->active_menu && hashmap_has(menu_get_windows_hash(), (void*)self->active_menu_wid)) && _entries == self->active_entry) {
|
||||
for (int y = _y; y < _y + MENU_BAR_HEIGHT; ++y) {
|
||||
for (int x = offset + 2; x < offset + 2 + w; ++x) {
|
||||
GFX(ctx, x, y) = rgb(93,163,236);
|
||||
}
|
||||
}
|
||||
}
|
||||
offset += draw_sdf_string(ctx, offset + 4, _y + 2, _entries->title, 16, rgb(255,255,255), SDF_FONT_THIN) + 10;
|
||||
_entries++;
|
||||
}
|
||||
}
|
||||
|
||||
void menu_bar_show_menu(yutani_t * yctx, yutani_window_t * window, struct menu_bar * self, int offset, struct menu_bar_entries * _entries) {
|
||||
struct MenuList * new_menu = menu_set_get_menu(self->set, _entries->action);
|
||||
menu_show(new_menu, yctx);
|
||||
yutani_window_move(yctx, new_menu->window, window->x + offset, window->y + self->y + MENU_BAR_HEIGHT);
|
||||
self->active_menu = new_menu;
|
||||
self->active_menu_wid = new_menu->window->wid;
|
||||
self->active_entry = _entries;
|
||||
if (self->redraw_callback) {
|
||||
self->redraw_callback();
|
||||
}
|
||||
}
|
||||
|
||||
int menu_bar_mouse_event(yutani_t * yctx, yutani_window_t * window, struct menu_bar * self, struct yutani_msg_window_mouse_event * me, int x, int y) {
|
||||
if (x < self->x || x >= self->x + self->width || y < self->y || y >= self->y + 24 /* base height */) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int offset = self->x;
|
||||
|
||||
struct menu_bar_entries * _entries = self->entries;
|
||||
|
||||
while (_entries->title) {
|
||||
int w = draw_sdf_string_width(_entries->title, 16, SDF_FONT_THIN) + 10;
|
||||
if (x >= offset && x < offset + w) {
|
||||
if (me->command == YUTANI_MOUSE_EVENT_CLICK || _close_enough(me)) {
|
||||
menu_bar_show_menu(yctx, window, self,offset,_entries);
|
||||
} else if (self->active_menu && hashmap_has(menu_get_windows_hash(), (void*)self->active_menu_wid) && _entries != self->active_entry) {
|
||||
menu_definitely_close(self->active_menu);
|
||||
menu_bar_show_menu(yctx, window, self,offset,_entries);
|
||||
}
|
||||
}
|
||||
|
||||
offset += w;
|
||||
_entries++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -32,7 +32,6 @@ class Classifier(object):
|
||||
'<toaru/sdf.h>': (None, '-ltoaru_sdf', ['<toaru/graphics.h>', '<toaru/hashmap.h>']),
|
||||
'<toaru/icon_cache.h>': (None, '-ltoaru_icon_cache', ['<toaru/graphics.h>', '<toaru/hashmap.h>']),
|
||||
'<toaru/menu.h>': (None, '-ltoaru_menu', ['<toaru/sdf.h>', '<toaru/yutani.h>', '<toaru/icon_cache.h>', '<toaru/graphics.h>', '<toaru/hashmap.h>']),
|
||||
'<toaru/menubar.h>': (None, '-ltoaru_menubar', ['<toaru/menu.h>', '<toaru/yutani.h>', '<toaru/icon_cache.h>', '<toaru/graphics.h>', '<toaru/hashmap.h>']),
|
||||
'<toaru/textregion.h>': (None, '-ltoaru_textregion', ['<toaru/sdf.h>', '<toaru/yutani.h>','<toaru/graphics.h>', '<toaru/hashmap.h>']),
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user