From df11ea4d11b2c103dacce85e62e30eefb845b737 Mon Sep 17 00:00:00 2001 From: daleclack Date: Sat, 23 Mar 2024 22:42:25 +0800 Subject: [PATCH] Add custom titlebar for wayland fix --- Gtkmm3_Mac/src/apps/FileWindow.hh | 4 ++ Gtkmm3_Mac/src/file_app/FileWindow.cc | 3 +- Gtkmm3_Mac/src/ui/MyDock.cc | 96 +++++++++++++++++++-------- Gtkmm3_Mac/src/ui/MyTitleBar.cc | 56 +++++++++++++++- Gtkmm3_Mac/src/ui/MyTitleBar.hh | 8 +++ 5 files changed, 135 insertions(+), 32 deletions(-) diff --git a/Gtkmm3_Mac/src/apps/FileWindow.hh b/Gtkmm3_Mac/src/apps/FileWindow.hh index 511427e..9ec2c05 100644 --- a/Gtkmm3_Mac/src/apps/FileWindow.hh +++ b/Gtkmm3_Mac/src/apps/FileWindow.hh @@ -1,6 +1,7 @@ #pragma once #include +#include "MyTitleBar.hh" enum class ViewMode{ MODE_ICON, @@ -31,6 +32,9 @@ private: Glib::RefPtr m_selection; Glib::RefPtr m_liststore; + // Title Bar + MyTitleBar title_bar; + //File Proprties Glib::RefPtr file_pixbuf; Glib::RefPtr folder_pixbuf; diff --git a/Gtkmm3_Mac/src/file_app/FileWindow.cc b/Gtkmm3_Mac/src/file_app/FileWindow.cc index 0e3cbfb..75a10cc 100644 --- a/Gtkmm3_Mac/src/file_app/FileWindow.cc +++ b/Gtkmm3_Mac/src/file_app/FileWindow.cc @@ -11,7 +11,8 @@ show_hidden("Show hidden files") //Initalize Window set_default_size(650,400); set_icon_name("file-app"); - set_title("File Manager"); + title_bar.set_ctrl_win(this); + title_bar.set_title("File Manager"); //Load Pixbufs auto pixbuf1 = Gdk::Pixbuf::create_from_resource("/org/gtk/daleclack/gnome-fs-regular.svg"); diff --git a/Gtkmm3_Mac/src/ui/MyDock.cc b/Gtkmm3_Mac/src/ui/MyDock.cc index 31a6850..c28d687 100644 --- a/Gtkmm3_Mac/src/ui/MyDock.cc +++ b/Gtkmm3_Mac/src/ui/MyDock.cc @@ -236,6 +236,25 @@ void MyDock::mydock_init(Gtk::Window *window, Gtk::Image *background1) prefs_win.set_background(background1); prefs_win.set_transient_for(*window); parent_win = window; + // Hide windows defaultly + prefs_win.set_visible(false); + prefs_win.set_transient_for(*window); + draw_app.set_visible(false); + draw_app.set_transient_for(*window); + file_app.set_visible(false); + file_app.set_transient_for(*window); + image_win.set_visible(false); + image_win.set_transient_for(*window); + editor_win.set_visible(false); + editor_win.set_transient_for(*window); + mine_win.set_visible(false); + mine_win.set_transient_for(*window); + game_win->set_visible(false); + game_win->set_transient_for(*window); + game24_win->set_visible(false); + game24_win->set_transient_for(*window); + calc_win->set_visible(false); + calc_win->set_transient_for(*window); } /* @@ -544,41 +563,60 @@ void MyDock::btnrun_clicked() btnlaunch_clicked(); } +// void MyDock::window_ctrl(Gtk::Window &window, bool on_dock) +// { +// // Get the GdkWindow object to get the state of a window +// auto gdk_win = window.get_window(); +// if (gdk_win) +// { +// /* +// The State of GdkWindow (GdkSurface for Gtk4) +// Gdk::WINDOW_STATE_WITHDRAWN: The window is not shown +// Gdk::WINDOW_STATE_ICONIFIED: The window is minimized +// the default mode for first launch +// */ +// auto state = gdk_win->get_state(); +// switch (state) +// { +// case Gdk::WINDOW_STATE_WITHDRAWN: +// window.present(); +// break; +// case Gdk::WINDOW_STATE_ICONIFIED: +// window.set_transient_for(*parent_win); +// window.deiconify(); +// break; +// default: +// if (on_dock) +// { +// window.unset_transient_for(); +// window.iconify(); +// break; +// } +// } +// } +// else +// { +// window.set_transient_for(*parent_win); +// window.present(); +// } +// } + void MyDock::window_ctrl(Gtk::Window &window, bool on_dock) { - // Get the GdkWindow object to get the state of a window - auto gdk_win = window.get_window(); - if (gdk_win) + // Use the visible status for window control + auto window_visible = window.get_visible(); + if (!window_visible) { - /* - The State of GdkWindow (GdkSurface for Gtk4) - Gdk::WINDOW_STATE_WITHDRAWN: The window is not shown - Gdk::WINDOW_STATE_ICONIFIED: The window is minimized - the default mode for first launch - */ - auto state = gdk_win->get_state(); - switch (state) - { - case Gdk::WINDOW_STATE_WITHDRAWN: - window.present(); - break; - case Gdk::WINDOW_STATE_ICONIFIED: - window.set_transient_for(*parent_win); - window.deiconify(); - break; - default: - if (on_dock) - { - window.unset_transient_for(); - window.iconify(); - break; - } - } + window.set_visible(); + window.present(); } else { - window.set_transient_for(*parent_win); - window.present(); + // The window only hide when click on dock + if (on_dock) + { + window.set_visible(false); + } } } diff --git a/Gtkmm3_Mac/src/ui/MyTitleBar.cc b/Gtkmm3_Mac/src/ui/MyTitleBar.cc index aff979b..7affa5a 100644 --- a/Gtkmm3_Mac/src/ui/MyTitleBar.cc +++ b/Gtkmm3_Mac/src/ui/MyTitleBar.cc @@ -2,5 +2,57 @@ MyTitleBar::MyTitleBar() { - -} \ No newline at end of file + // Load Css Provider + provider = Gtk::CssProvider::create(); + provider->load_from_resource("/org/gtk/daleclack/title_style.css"); + + // Add Buttons + btn_close.set_image_from_icon_name("window-close-symbolic"); + btn_close.set_hexpand(false); + btn_close.set_vexpand(false); + btn_mini.set_image_from_icon_name("window-minimize-symbolic"); + btn_mini.set_hexpand(false); + btn_mini.set_vexpand(false); + header.pack_start(btn_close); + header.pack_start(btn_mini); + btn_close.signal_clicked().connect(sigc::mem_fun(*this, &MyTitleBar::ctrl_window_close)); + btn_mini.signal_clicked().connect(sigc::mem_fun(*this, &MyTitleBar::ctrl_window_hide)); + + // Add title widget + header.set_custom_title(title_label); + + // Add class for header bar + auto style1 = header.get_style_context(); + style1->add_class("mytitlebar"); + style1->add_provider(provider, G_MAXUINT); + + // Add class for buttons + auto style2 = btn_close.get_style_context(); + style2->add_class("ctrl_btn"); + style2->add_provider(provider, G_MAXUINT); + + auto style3 = btn_mini.get_style_context(); + style2->add_class("ctrl_btn"); + style3->add_provider(provider, G_MAXUINT); +} + +void MyTitleBar::ctrl_window_close() +{ + ctrl_win->close(); +} + +void MyTitleBar::ctrl_window_hide() +{ + ctrl_win->hide(); +} + +void MyTitleBar::set_title(const Glib::ustring &title) +{ + title_label.set_label(title); +} + +void MyTitleBar::set_ctrl_win(Gtk::Window *window) +{ + window->set_titlebar(header); + ctrl_win = window; +} diff --git a/Gtkmm3_Mac/src/ui/MyTitleBar.hh b/Gtkmm3_Mac/src/ui/MyTitleBar.hh index efd0afe..bb04515 100644 --- a/Gtkmm3_Mac/src/ui/MyTitleBar.hh +++ b/Gtkmm3_Mac/src/ui/MyTitleBar.hh @@ -6,6 +6,14 @@ class MyTitleBar { public: MyTitleBar(); + void set_ctrl_win(Gtk::Window *window); + void set_title(const Glib::ustring &title); Gtk::HeaderBar header; private: + Glib::RefPtr provider; + Gtk::Button btn_close, btn_mini; + Gtk::Label title_label; + Gtk::Window *ctrl_win; + void ctrl_window_close(); + void ctrl_window_hide(); };