Add custom titlebar for wayland fix

This commit is contained in:
daleclack 2024-03-23 22:42:25 +08:00
parent e9688241dd
commit df11ea4d11
5 changed files with 135 additions and 32 deletions

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <gtkmm.h> #include <gtkmm.h>
#include "MyTitleBar.hh"
enum class ViewMode{ enum class ViewMode{
MODE_ICON, MODE_ICON,
@ -31,6 +32,9 @@ private:
Glib::RefPtr<Gtk::TreeSelection> m_selection; Glib::RefPtr<Gtk::TreeSelection> m_selection;
Glib::RefPtr<Gtk::ListStore> m_liststore; Glib::RefPtr<Gtk::ListStore> m_liststore;
// Title Bar
MyTitleBar title_bar;
//File Proprties //File Proprties
Glib::RefPtr<Gdk::Pixbuf> file_pixbuf; Glib::RefPtr<Gdk::Pixbuf> file_pixbuf;
Glib::RefPtr<Gdk::Pixbuf> folder_pixbuf; Glib::RefPtr<Gdk::Pixbuf> folder_pixbuf;

View File

@ -11,7 +11,8 @@ show_hidden("Show hidden files")
//Initalize Window //Initalize Window
set_default_size(650,400); set_default_size(650,400);
set_icon_name("file-app"); set_icon_name("file-app");
set_title("File Manager"); title_bar.set_ctrl_win(this);
title_bar.set_title("File Manager");
//Load Pixbufs //Load Pixbufs
auto pixbuf1 = Gdk::Pixbuf::create_from_resource("/org/gtk/daleclack/gnome-fs-regular.svg"); auto pixbuf1 = Gdk::Pixbuf::create_from_resource("/org/gtk/daleclack/gnome-fs-regular.svg");

View File

@ -236,6 +236,25 @@ void MyDock::mydock_init(Gtk::Window *window, Gtk::Image *background1)
prefs_win.set_background(background1); prefs_win.set_background(background1);
prefs_win.set_transient_for(*window); prefs_win.set_transient_for(*window);
parent_win = 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(); 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) void MyDock::window_ctrl(Gtk::Window &window, bool on_dock)
{ {
// Get the GdkWindow object to get the state of a window // Use the visible status for window control
auto gdk_win = window.get_window(); auto window_visible = window.get_visible();
if (gdk_win) if (!window_visible)
{ {
/* window.set_visible();
The State of GdkWindow (GdkSurface for Gtk4) window.present();
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 else
{ {
window.set_transient_for(*parent_win); // The window only hide when click on dock
window.present(); if (on_dock)
{
window.set_visible(false);
}
} }
} }

View File

@ -2,5 +2,57 @@
MyTitleBar::MyTitleBar() MyTitleBar::MyTitleBar()
{ {
// 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;
}

View File

@ -6,6 +6,14 @@ class MyTitleBar
{ {
public: public:
MyTitleBar(); MyTitleBar();
void set_ctrl_win(Gtk::Window *window);
void set_title(const Glib::ustring &title);
Gtk::HeaderBar header; Gtk::HeaderBar header;
private: private:
Glib::RefPtr<Gtk::CssProvider> provider;
Gtk::Button btn_close, btn_mini;
Gtk::Label title_label;
Gtk::Window *ctrl_win;
void ctrl_window_close();
void ctrl_window_hide();
}; };