[digesting_libdecor] simplifying main loop logic ...

main
Allen Webster 2026-03-02 17:36:35 -08:00
parent 7103c61c71
commit 8dfe2fc72e
1 changed files with 91 additions and 179 deletions

View File

@ -337,15 +337,6 @@ const struct zxdg_toplevel_decoration_v1_listener
xdg_toplevel_decoration_listener = { xdg_toplevel_decoration_configure };
static GtkWidget* find_widget_by_type(GtkWidget *root, enum header_element type);
static void ensure_component(struct border_component *cmpnt);
static void
hide_border_component(struct border_component *border_component){
if (border_component->wl_surface != 0){
wl_surface_attach(border_component->wl_surface, 0, 0, 0);
wl_surface_commit(border_component->wl_surface);
}
}
static void
border_component_reallocate(struct border_component *component, int w, int h){
@ -903,47 +894,41 @@ int main(){
if (csd && !ctx.csd_active){
ctx.csd_active = 1;
ensure_component(&ctx.component_slot[COMPONENT_SLOT_SHADOW]);
ensure_component(&ctx.component_slot[COMPONENT_SLOT_HEADER]);
{
if (ctx.component_slot[COMPONENT_SLOT_SHADOW].wl_surface != 0){
wl_subsurface_place_above(ctx.component_slot[COMPONENT_SLOT_HEADER].wl_subsurface,
ctx.component_slot[COMPONENT_SLOT_SHADOW].wl_surface);
for (int slot = 1; slot < COMPONENT_SLOT_COUNT; slot += 1){
struct border_component *cmpnt = &ctx.component_slot[slot];
if (cmpnt->wl_surface == 0){
cmpnt->wl_surface = wl_compositor_create_surface(ctx.wl_compositor);
wl_proxy_set_tag((struct wl_proxy *)cmpnt->wl_surface, &libdecor_gtk_proxy_tag);
cmpnt->wl_subsurface = wl_subcompositor_get_subsurface(ctx.wl_subcompositor, cmpnt->wl_surface, ctx.wl_surface);
}
if (GTK_IS_WIDGET(ctx.header)){
gtk_widget_destroy(ctx.header);
ctx.header = 0;
}
if (GTK_IS_WIDGET(ctx.window)){
gtk_widget_destroy(ctx.window);
ctx.window = 0;
}
ctx.window = gtk_offscreen_window_new();
ctx.header = gtk_header_bar_new();
g_object_get(gtk_widget_get_settings(ctx.window),
"gtk-double-click-time", &ctx.double_click_time_ms,
NULL);
g_object_set(ctx.header,
"title", ctx.title,
"has-subtitle", FALSE,
"show-close-button", TRUE,
NULL);
GtkStyleContext *context_hdr = gtk_widget_get_style_context(ctx.header);
gtk_style_context_add_class(context_hdr, GTK_STYLE_CLASS_TITLEBAR);
gtk_style_context_add_class(context_hdr, "default-decoration");
gtk_window_set_titlebar(GTK_WINDOW(ctx.window), ctx.header);
gtk_header_bar_set_show_close_button(GTK_HEADER_BAR(ctx.header), TRUE);
gtk_window_set_resizable(GTK_WINDOW(ctx.window), (ctx.frame_capabilities & LIBDECOR_ACTION_RESIZE) != 0);
}
wl_subsurface_place_above(ctx.component_slot[COMPONENT_SLOT_HEADER].wl_subsurface,
ctx.component_slot[COMPONENT_SLOT_SHADOW].wl_surface);
ctx.header = gtk_header_bar_new();
ctx.window = gtk_offscreen_window_new();
g_object_get(gtk_widget_get_settings(ctx.window),
"gtk-double-click-time", &ctx.double_click_time_ms,
NULL);
g_object_set(ctx.header,
"title", ctx.title,
"has-subtitle", FALSE,
"show-close-button", TRUE,
NULL);
GtkStyleContext *context_hdr = gtk_widget_get_style_context(ctx.header);
gtk_style_context_add_class(context_hdr, GTK_STYLE_CLASS_TITLEBAR);
gtk_style_context_add_class(context_hdr, "default-decoration");
gtk_window_set_titlebar(GTK_WINDOW(ctx.window), ctx.header);
gtk_header_bar_set_show_close_button(GTK_HEADER_BAR(ctx.header), TRUE);
}
if (!csd && ctx.csd_active){
ctx.csd_active = 0;
if (ctx.header != 0){
gtk_widget_destroy(ctx.header);
ctx.header = 0;
@ -977,119 +962,50 @@ int main(){
}
}
enum decoration_type decoration_type = DECORATION_TYPE_ALL;
if (ctx.frame_window_state & LIBDECOR_WINDOW_STATE_FULLSCREEN){
decoration_type = DECORATION_TYPE_NONE;
}
else if (ctx.frame_window_state & (LIBDECOR_WINDOW_STATE_MAXIMIZED |
LIBDECOR_WINDOW_STATE_TILED_LEFT |
LIBDECOR_WINDOW_STATE_TILED_RIGHT |
LIBDECOR_WINDOW_STATE_TILED_TOP |
LIBDECOR_WINDOW_STATE_TILED_BOTTOM)){
decoration_type = DECORATION_TYPE_TITLE_ONLY;
}
bool shadow = 0;
bool title_bar = 0;
Sides2D csd_size = {0};
if (csd){
switch (decoration_type){
enum decoration_type csd_decoration_type = DECORATION_TYPE_ALL;
if (ctx.frame_window_state & LIBDECOR_WINDOW_STATE_FULLSCREEN){
csd_decoration_type = DECORATION_TYPE_NONE;
}
else if (ctx.frame_window_state & (LIBDECOR_WINDOW_STATE_MAXIMIZED |
LIBDECOR_WINDOW_STATE_TILED_LEFT |
LIBDECOR_WINDOW_STATE_TILED_RIGHT |
LIBDECOR_WINDOW_STATE_TILED_TOP |
LIBDECOR_WINDOW_STATE_TILED_BOTTOM)){
csd_decoration_type = DECORATION_TYPE_TITLE_ONLY;
}
switch (csd_decoration_type){
case DECORATION_TYPE_NONE: break;
case DECORATION_TYPE_ALL: { shadow = 1; title_bar = 1; }break;
case DECORATION_TYPE_TITLE_ONLY: { title_bar = 1; }break;
}
if (shadow){
//ensure_component(&ctx.component_slot[COMPONENT_SLOT_SHADOW]);
}
else{
hide_border_component(&ctx.component_slot[COMPONENT_SLOT_SHADOW]);
}
if (title_bar){
//ensure_component(&ctx.component_slot[COMPONENT_SLOT_HEADER]);
}
else{
hide_border_component(&ctx.component_slot[COMPONENT_SLOT_HEADER]);
}
}
if (csd){
if (title_bar){
#if 0
if (ctx.header == 0){
if (ctx.component_slot[COMPONENT_SLOT_SHADOW].wl_surface != 0){
wl_subsurface_place_above(ctx.component_slot[COMPONENT_SLOT_HEADER].wl_subsurface,
ctx.component_slot[COMPONENT_SLOT_SHADOW].wl_surface);
}
if (GTK_IS_WIDGET(ctx.header)){
gtk_widget_destroy(ctx.header);
ctx.header = 0;
}
if (GTK_IS_WIDGET(ctx.window)){
gtk_widget_destroy(ctx.window);
ctx.window = 0;
}
ctx.window = gtk_offscreen_window_new();
ctx.header = gtk_header_bar_new();
g_object_get(gtk_widget_get_settings(ctx.window),
"gtk-double-click-time", &ctx.double_click_time_ms,
NULL);
g_object_set(ctx.header,
"title", ctx.title,
"has-subtitle", FALSE,
"show-close-button", TRUE,
NULL);
GtkStyleContext *context_hdr = gtk_widget_get_style_context(ctx.header);
gtk_style_context_add_class(context_hdr, GTK_STYLE_CLASS_TITLEBAR);
gtk_style_context_add_class(context_hdr, "default-decoration");
gtk_window_set_titlebar(GTK_WINDOW(ctx.window), ctx.header);
gtk_header_bar_set_show_close_button(GTK_HEADER_BAR(ctx.header), TRUE);
gtk_window_set_resizable(GTK_WINDOW(ctx.window), (ctx.frame_capabilities & LIBDECOR_ACTION_RESIZE) != 0);
if (!shadow){
struct border_component *cmpnt = &ctx.component_slot[COMPONENT_SLOT_SHADOW];
if (cmpnt->wl_surface != 0){
wl_surface_attach(cmpnt->wl_surface, 0, 0, 0);
wl_surface_commit(cmpnt->wl_surface);
}
#endif
}
}
Sides2D csd_size = {0};
if (csd){
if (title_bar){
gtk_widget_show_all(ctx.window);
gtk_widget_get_preferred_height(ctx.header, 0, &csd_size.y[0]);
}
}
if (ctx.has_cached_config){
if (ctx.cached_config.initialized &&
ctx.cached_config.window_width != 0 &&
ctx.cached_config.window_height != 0){
int w = ctx.cached_config.window_width;
int h = ctx.cached_config.window_height;
if (csd){
w -= csd_size.x[0] + csd_size.x[1];
h -= csd_size.y[0] + csd_size.y[1];
if (!title_bar){
struct border_component *cmpnt = &ctx.component_slot[COMPONENT_SLOT_HEADER];
if (cmpnt->wl_surface != 0){
wl_surface_attach(cmpnt->wl_surface, 0, 0, 0);
wl_surface_commit(cmpnt->wl_surface);
}
if (!(ctx.cached_config.window_state & LIBDECOR_WINDOW_STATE_NON_FLOATING)){
w = CLAMP_(ctx.size_bounds.x[0], w, ctx.size_bounds.x[1]);
h = CLAMP_(ctx.size_bounds.y[0], h, ctx.size_bounds.y[1]);
}
ctx.w = w;
ctx.h = h;
}
}
if (csd){
if (title_bar){
enum libdecor_window_state state = ctx.frame_window_state;
gtk_window_set_resizable(GTK_WINDOW(ctx.window), (ctx.frame_capabilities & LIBDECOR_ACTION_RESIZE) != 0);
gtk_widget_show_all(ctx.window);
gtk_widget_get_preferred_height(ctx.header, 0, &csd_size.y[0]);
if (!(state & LIBDECOR_WINDOW_STATE_ACTIVE)){
gtk_widget_set_state_flags(ctx.window, GTK_STATE_FLAG_BACKDROP, true);
}
@ -1104,36 +1020,21 @@ int main(){
gtk_style_context_add_class(style, "maximized");
}
gtk_widget_show_all(ctx.window);
int preferred_min_width;
{
gtk_header_bar_set_title(GTK_HEADER_BAR(ctx.header), "");
gtk_widget_get_preferred_width(ctx.header, NULL, &preferred_min_width);
gtk_header_bar_set_title(GTK_HEADER_BAR(ctx.header), ctx.title);
}
ctx.size_bounds.x[0] = CLAMP_BOT(ctx.size_bounds.x[0], preferred_min_width);
ctx.size_bounds.x[1] = CLAMP_BOT(ctx.size_bounds.x[1], ctx.size_bounds.x[0]);
}
}
if (!(ctx.frame_capabilities & LIBDECOR_ACTION_RESIZE)){
xdg_toplevel_set_min_size(ctx.xdg_toplevel, ctx.w, ctx.h);
xdg_toplevel_set_max_size(ctx.xdg_toplevel, ctx.w, ctx.h);
}
else{
int csd_added_w = csd_size.x[0] + csd_size.x[1];
int csd_added_h = csd_size.y[0] + csd_size.y[1];
for (int i = 0; i < 2; i += 1){
int mw = ctx.size_bounds.x[i] + csd_added_w;
int mh = ctx.size_bounds.y[i] + csd_added_h;
if (i == 0){
xdg_toplevel_set_min_size(ctx.xdg_toplevel, mw, mh);
}
else{
xdg_toplevel_set_max_size(ctx.xdg_toplevel, mw, mh);
if (ctx.has_cached_config){
if (ctx.cached_config.initialized &&
ctx.cached_config.window_width != 0 &&
ctx.cached_config.window_height != 0){
int w = ctx.cached_config.window_width;
int h = ctx.cached_config.window_height;
if (csd){
w -= csd_size.x[0] + csd_size.x[1];
h -= csd_size.y[0] + csd_size.y[1];
}
ctx.w = w;
ctx.h = h;
}
}
@ -1274,6 +1175,25 @@ int main(){
}
}
if (!(ctx.frame_capabilities & LIBDECOR_ACTION_RESIZE)){
xdg_toplevel_set_min_size(ctx.xdg_toplevel, ctx.w, ctx.h);
xdg_toplevel_set_max_size(ctx.xdg_toplevel, ctx.w, ctx.h);
}
else{
int csd_added_w = csd_size.x[0] + csd_size.x[1];
int csd_added_h = csd_size.y[0] + csd_size.y[1];
for (int i = 0; i < 2; i += 1){
int mw = ctx.size_bounds.x[i] + csd_added_w;
int mh = ctx.size_bounds.y[i] + csd_added_h;
if (i == 0){
xdg_toplevel_set_min_size(ctx.xdg_toplevel, mw, mh);
}
else{
xdg_toplevel_set_max_size(ctx.xdg_toplevel, mw, mh);
}
}
}
{
Extent2D extent = {0};
extent.w = ctx.w;
@ -1292,6 +1212,7 @@ int main(){
if (ctx.has_cached_config){
ctx.has_cached_config = 0;
memset(&ctx.cached_config, 0, sizeof ctx.cached_config);
xdg_surface_ack_configure(ctx.xdg_surface, ctx.cached_config.serial);
}
@ -1777,15 +1698,6 @@ component_slot_from_wl_surface(const struct wl_surface *surface){
return(result);
}
static void
ensure_component(struct border_component *cmpnt){
if (cmpnt->wl_surface == 0){
cmpnt->wl_surface = wl_compositor_create_surface(ctx.wl_compositor);
wl_proxy_set_tag((struct wl_proxy *)cmpnt->wl_surface, &libdecor_gtk_proxy_tag);
cmpnt->wl_subsurface = wl_subcompositor_get_subsurface(ctx.wl_subcompositor, cmpnt->wl_surface, ctx.wl_surface);
}
}
static void
draw_header_button(cairo_t *cr, cairo_surface_t *surface,
enum header_element button_type){