Add custom titlebar for Calc App

This commit is contained in:
daleclack 2024-03-25 23:52:51 +08:00
parent aa221886b1
commit bfa8678005
3 changed files with 50 additions and 29 deletions

View File

@ -55,6 +55,7 @@ set(RESOURCE_LIST
style.css
style_dark.css
mine_app.css
title_style.css
folder.svg
folder-images.svg
image_file.svg

View File

@ -1,11 +1,13 @@
#include "CalcApp.h"
#include "calc.h"
#include "MyTitleBar.h"
#include <string>
#include <iostream>
struct _CalcApp
{
GtkWindow parent_instance;
MyTitleBar *title_bar;
GtkWidget *entry_ans; // Child widgets
GtkWidget *btnanswer, *btnback, *btnclear;
GtkWidget *btn0, *btn1, *btn2, *btn3, *btn4,
@ -103,8 +105,10 @@ static void calc_app_init(CalcApp *self)
{
gtk_widget_init_template(GTK_WIDGET(self));
// Initalize window
self->title_bar = my_titlebar_new();
gtk_window_set_title(GTK_WINDOW(self), "Calculator");
gtk_window_set_icon_name(GTK_WINDOW(self), "calcapp");
my_titlebar_set_window(self->title_bar, GTK_WIDGET(self));
// Link Signals
g_signal_connect(self->btnanswer, "clicked", G_CALLBACK(btnanswer_clicked), self);

View File

@ -96,41 +96,57 @@ static void pressed(GtkGesture *gesture, int n_press,
}
// Window control func, X11/Windows Only!
// static void window_ctrl(GtkWindow *window, GtkWindow *parent)
// {
// // Get GdkSurface for window state
// GdkSurface *surface = gtk_native_get_surface(GTK_NATIVE(window));
// if (surface)
// {
// // The state will available when the window open
// ushort state = gdk_toplevel_get_state(GDK_TOPLEVEL(surface));
// static ushort state1 = state;
// g_print("%d\n", state);
// state -= state1;
// switch (state)
// {
// // Minimized
// case GDK_TOPLEVEL_STATE_MINIMIZED:
// g_print("Try to unminimize");
// gtk_window_set_transient_for(window, parent);
// gtk_window_unminimize(window);
// #ifdef WAYLAND_FIX
// // Fix for non-x11 environments
// gtk_window_present(window);
// #endif
// break;
// default:
// // The controlled window is on dock
// gtk_window_set_transient_for(window, NULL);
// gtk_window_minimize(window);
// break;
// }
// }
// else
// {
// // Create a window
// gtk_window_set_transient_for(window, parent);
// gtk_window_present(window);
// }
// }
static void window_ctrl(GtkWindow *window, GtkWindow *parent)
{
// Get GdkSurface for window state
GdkSurface *surface = gtk_native_get_surface(GTK_NATIVE(window));
if (surface)
gboolean visible = gtk_widget_get_visible(GTK_WIDGET(window));
// The unminimize if changed to hide
if (!visible)
{
// The state will available when the window open
ushort state = gdk_toplevel_get_state(GDK_TOPLEVEL(surface));
static ushort state1 = state;
g_print("%d\n", state);
state -= state1;
switch (state)
{
// Minimized
case GDK_TOPLEVEL_STATE_MINIMIZED:
g_print("Try to unminimize");
gtk_window_set_transient_for(window, parent);
gtk_window_unminimize(window);
#ifdef WAYLAND_FIX
// Fix for non-x11 environments
gtk_window_present(window);
#endif
break;
default:
// The controlled window is on dock
gtk_window_set_transient_for(window, NULL);
gtk_window_minimize(window);
break;
}
gtk_widget_set_visible(GTK_WIDGET(window), TRUE);
gtk_window_present(window);
}
else
{
// Create a window
gtk_window_set_transient_for(window, parent);
gtk_window_present(window);
// Window control only available for icons on dock
gtk_widget_set_visible(GTK_WIDGET(window), FALSE);
}
}