Skip to content

Commit

Permalink
#536: don't show headerbar and traditional menu at the same time; sup…
Browse files Browse the repository at this point in the history
…port headerbar for peer/group dialog (#537)

* DialogPeer can use headerbar

* move HeaderBar to uihelper

* DialogGroup can use headerbar

* fix style
  • Loading branch information
lidaobing committed Apr 14, 2024
1 parent 94f2a39 commit fef69ff
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 55 deletions.
44 changes: 25 additions & 19 deletions src/iptux/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Application::Application(shared_ptr<IptuxConfig> config)
notificationService = new TerminalNotifierNoticationService();
#else
notificationService = new GioNotificationService();
use_header_bar_ = true;
// GError* error = nullptr;
// if(!g_application_register(G_APPLICATION(app), nullptr, &error)) {
// LOG_WARN("g_application_register failed: %s-%d-%s",
Expand Down Expand Up @@ -126,6 +127,30 @@ void Application::onStartup(Application& self) {
self.data = make_shared<ProgramData>(self.config);
self.logSystem = new LogSystem(self.data);
self.cthrd = make_shared<UiCoreThread>(&self, self.data);

bool use_app_menu = true;
#if SYSTEM_DARWIN
#else
use_app_menu = gtk_application_prefers_app_menu(self.app);
#endif

if(use_app_menu) {
auto app_menu =
G_MENU_MODEL(gtk_builder_get_object(self.menuBuilder, "appmenu"));
gtk_application_set_app_menu(GTK_APPLICATION(self.app), app_menu);
self.menu_ =
G_MENU_MODEL(gtk_builder_get_object(self.menuBuilder, "menubar-when-app-menu"));
if(!self.use_header_bar()) {
gtk_application_set_menubar(GTK_APPLICATION(self.app), self.menu());
}
} else {
self.menu_ =
G_MENU_MODEL(gtk_builder_get_object(self.menuBuilder, "menubar-when-no-app-menu"));
if(!self.use_header_bar()) {
gtk_application_set_menubar(GTK_APPLICATION(self.app), self.menu());
}
}

self.window = new MainWindow(&self, *self.cthrd);
self.eventAdaptor = new EventAdaptor(
self.cthrd->signalEvent,
Expand Down Expand Up @@ -154,25 +179,6 @@ void Application::onStartup(Application& self) {
g_action_map_add_action_entries(G_ACTION_MAP(self.app), app_entries,
G_N_ELEMENTS(app_entries), &self);

bool use_app_menu = true;
#if SYSTEM_DARWIN
#else
use_app_menu = gtk_application_prefers_app_menu(self.app);
#endif

if(use_app_menu) {
auto app_menu =
G_MENU_MODEL(gtk_builder_get_object(self.menuBuilder, "appmenu"));
gtk_application_set_app_menu(GTK_APPLICATION(self.app), app_menu);
auto menubar =
G_MENU_MODEL(gtk_builder_get_object(self.menuBuilder, "menubar-when-app-menu"));
gtk_application_set_menubar(GTK_APPLICATION(self.app), menubar);
} else {
auto menubar =
G_MENU_MODEL(gtk_builder_get_object(self.menuBuilder, "menubar-when-no-app-menu"));
gtk_application_set_menubar(GTK_APPLICATION(self.app), menubar);
}

add_accelerator(self.app, "app.quit", "<Primary>Q");
add_accelerator(self.app, "win.refresh", "F5");
add_accelerator(self.app, "win.detect", "<Primary>D");
Expand Down
4 changes: 4 additions & 0 deletions src/iptux/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ class Application {
LogSystem* getLogSystem() { return logSystem; }
std::shared_ptr<ProgramData> getProgramData() { return data; }
std::shared_ptr<UiCoreThread> getCoreThread() { return cthrd; }
bool use_header_bar() { return use_header_bar_; }
void refreshTransTasks();
PPalInfo getMe();
GMenuModel* menu() { return menu_; }

private:
std::shared_ptr<IptuxConfig> config;
Expand All @@ -52,6 +54,8 @@ class Application {
EventAdaptor* eventAdaptor = 0;
LogSystem* logSystem = 0;
NotificationService* notificationService = 0;
GMenuModel* menu_ = 0;
bool use_header_bar_ = false;
bool started{false};

public:
Expand Down
16 changes: 13 additions & 3 deletions src/iptux/DialogGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ DialogGroup* DialogGroup::GroupDialogEntry(Application* app,

dlggrp = new DialogGroup(app, grpinf);
window = GTK_WIDGET(dlggrp->CreateMainWindow());
dlggrp->CreateTitle();
gtk_container_add(GTK_CONTAINER(window), dlggrp->CreateAllArea());
gtk_widget_show_all(window);

Expand Down Expand Up @@ -195,10 +196,8 @@ void DialogGroup::SaveUILayout() {
* @return 窗口
*/
GtkWindow* DialogGroup::CreateMainWindow() {
char buf[MAX_BUFLEN];
window = GTK_APPLICATION_WINDOW(gtk_application_window_new(app->getApp()));
snprintf(buf, MAX_BUFLEN, _("Talk with the group %s"), grpinf->name.c_str());
gtk_window_set_title(GTK_WINDOW(window), buf);
gtk_window_set_title(GTK_WINDOW(window), GetTitle().c_str());
gtk_window_set_default_size(GTK_WINDOW(window),
config->GetInt("group_window_width", 500),
config->GetInt("group_window_height", 350));
Expand Down Expand Up @@ -717,4 +716,15 @@ void DialogGroup::onSortType(GSimpleAction* action,
g_simple_action_set_state(action, value);
}

void DialogGroup::CreateTitle() {
if(app->use_header_bar()) {
GtkHeaderBar* header_bar = CreateHeaderBar(GTK_WINDOW(window), app->menu());
gtk_header_bar_set_title(header_bar, GetTitle().c_str());
}
}

string DialogGroup::GetTitle() {
return stringFormat(_("Talk with the group %s"), grpinf->name.c_str());
}

} // namespace iptux
2 changes: 2 additions & 0 deletions src/iptux/DialogGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class DialogGroup : public DialogBase {
void SaveUILayout();

GtkWindow* CreateMainWindow();
void CreateTitle();
GtkWidget* CreateAllArea();

GtkWidget* CreateMemberArea();
Expand All @@ -57,6 +58,7 @@ class DialogGroup : public DialogBase {
bool SendTextMsg() override;
void BroadcastEnclosureMsg(const std::vector<FileInfo*>& files) override;
void BroadcastTextMsg(const gchar* msg);
std::string GetTitle();

static GtkWidget* CreatePopupMenu(GtkTreeModel* model);
//回调处理部分
Expand Down
17 changes: 14 additions & 3 deletions src/iptux/DialogPeer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ void DialogPeer::init() {
auto dlgpr = this;
auto window = GTK_WIDGET(dlgpr->CreateMainWindow());
grpinf->setDialogBase(this);
CreateTitle();
gtk_container_add(GTK_CONTAINER(window), dlgpr->CreateAllArea());
gtk_widget_show_all(window);
gtk_widget_grab_focus(GTK_WIDGET(inputTextviewWidget));
Expand Down Expand Up @@ -127,12 +128,15 @@ void DialogPeer::UpdatePalData(PalInfo* pal) {
refreshTitle();
}

void DialogPeer::refreshTitle() {
string DialogPeer::GetTitle() {
auto palinfor = grpinf->getMembers()[0].get();
auto title = stringFormat(
return stringFormat(
_("Talk with %s(%s) IP:%s"), palinfor->getName().c_str(),
palinfor->getHost().c_str(), inAddrToString(palinfor->ipv4).c_str());
gtk_window_set_title(GTK_WINDOW(window), title.c_str());
}

void DialogPeer::refreshTitle() {
gtk_window_set_title(GTK_WINDOW(window), GetTitle().c_str());
}

/**
Expand Down Expand Up @@ -233,6 +237,13 @@ GtkWindow* DialogPeer::CreateMainWindow() {
return GTK_WINDOW(window);
}

void DialogPeer::CreateTitle() {
if(app->use_header_bar()) {
GtkHeaderBar* headerBar = CreateHeaderBar(GTK_WINDOW(window), app->menu());
gtk_header_bar_set_title(headerBar, GetTitle().c_str());
}
}

/**
* 创建所有区域.
* @return 主窗体
Expand Down
2 changes: 2 additions & 0 deletions src/iptux/DialogPeer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class DialogPeer : public DialogBase {
void init();

GtkWindow* CreateMainWindow();
void CreateTitle();
GtkWidget* CreateAllArea();

GtkWidget* CreateInfoArea();
Expand All @@ -65,6 +66,7 @@ class DialogPeer : public DialogBase {
void FeedbackMsg(const std::vector<ChipData>& dtlist);
MsgPara* PackageMsg(const std::vector<ChipData>& dtlist);
void refreshSendAction();
std::string GetTitle();

//回调处理部分
private:
Expand Down
22 changes: 10 additions & 12 deletions src/iptux/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ void MainWindow::CreateWindow() {

/* 创建主窗口 */
window = CreateMainWindow();
CreateTitle();
gtk_container_add(GTK_CONTAINER(window), CreateAllArea());
gtk_widget_show_all(window);

Expand Down Expand Up @@ -398,6 +399,7 @@ void MainWindow::ClearSublayer() {
g_object_unref(accel);
if (timerid > 0)
g_source_remove(timerid);
g_object_unref(builder);
}

/**
Expand Down Expand Up @@ -445,6 +447,14 @@ string MainWindow::getTitle() const {
}
}


void MainWindow::CreateTitle() {
if(app->use_header_bar()) {
GtkHeaderBar* headerBar = CreateHeaderBar(GTK_WINDOW(window), app->menu());
gtk_header_bar_set_title(headerBar, getTitle().c_str());
}
}

/**
* 创建所有区域.
* @return 主窗体
Expand All @@ -454,18 +464,6 @@ GtkWidget* MainWindow::CreateAllArea() {

box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);

#if SYSTEM_DARWIN
#else
GtkHeaderBar* headerBar =
GTK_HEADER_BAR(gtk_builder_get_object(builder, "header_bar"));
gtk_header_bar_set_title(headerBar, getTitle().c_str());
gtk_window_set_titlebar(GTK_WINDOW(window), GTK_WIDGET(headerBar));
auto menuButton = gtk_builder_get_object(builder, "menu_button");
gtk_menu_button_set_menu_model(
GTK_MENU_BUTTON(menuButton),
G_MENU_MODEL(gtk_builder_get_object(app->getMenuBuilder(), "menubar-when-no-app-menu")));
#endif

gtk_box_pack_start(GTK_BOX(box), CreateToolBar(), FALSE, FALSE, 0);

paned = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
Expand Down
1 change: 1 addition & 0 deletions src/iptux/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class MainWindow : public sigc::trackable {

void CreateWindow();
GtkWidget* CreateMainWindow();
void CreateTitle();
GtkWidget* CreateAllArea();

GtkWidget* CreateToolBar();
Expand Down
14 changes: 14 additions & 0 deletions src/iptux/UiHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,18 @@ gboolean gtk_window_iconify_on_delete(GtkWindow* window) {
return TRUE;
}

GtkHeaderBar* CreateHeaderBar(GtkWindow* window, GMenuModel* menu) {
GtkBuilder* builder =
gtk_builder_new_from_resource(IPTUX_RESOURCE "gtk/HeaderBar.ui");
GtkHeaderBar* headerBar =
GTK_HEADER_BAR(gtk_builder_get_object(builder, "header_bar"));
gtk_header_bar_set_has_subtitle(headerBar, FALSE);
auto menuButton = gtk_builder_get_object(builder, "menu_button");
gtk_menu_button_set_menu_model(GTK_MENU_BUTTON(menuButton), menu);
gtk_window_set_titlebar(window, GTK_WIDGET(headerBar));

g_object_unref(builder);
return headerBar;
}

} // namespace iptux
9 changes: 9 additions & 0 deletions src/iptux/UiHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,14 @@ GActionEntry makeStateActionEntry(const std::string& name,
*/
std::string markupEscapeText(const std::string& str);

/**
* @brief create a headerbar with menu, and set this headerbar to the window
*
* we need to set to window inside this func, otherwise we need manage the refcount
* manually.
*
*/
GtkHeaderBar* CreateHeaderBar(GtkWindow* window, GMenuModel* menu);

} // namespace iptux
#endif // IPTUX_UIHELPER_H
22 changes: 22 additions & 0 deletions src/iptux/resources/gtk/HeaderBar.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkHeaderBar" id="header_bar">
<property name="visible">True</property>
<property name="show-close-button">True</property>
<child>
<object class="GtkMenuButton" id="menu_button">
<property name="visible">True</property>
<property name="use_popover">False</property>
<property name="image">menu_image</property>
</object>
<packing>
<property name="pack_type">end</property>
</packing>
</child>
</object>
<object class="GtkImage" id="menu_image">
<property name="visible">True</property>
<property name="icon_name">open-menu-symbolic</property>
</object>
</interface>
18 changes: 0 additions & 18 deletions src/iptux/resources/gtk/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,4 @@
</item>
</section>
</menu>
<object class="GtkHeaderBar" id="header_bar">
<property name="visible">True</property>
<property name="show-close-button">True</property>
<child>
<object class="GtkMenuButton" id="menu_button">
<property name="visible">True</property>
<property name="use_popover">False</property>
<property name="image">menu_image</property>
</object>
<packing>
<property name="pack_type">end</property>
</packing>
</child>
</object>
<object class="GtkImage" id="menu_image">
<property name="visible">True</property>
<property name="icon_name">open-menu-symbolic</property>
</object>
</interface>
1 change: 1 addition & 0 deletions src/iptux/resources/iptux.gresource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<gresource prefix="/io/github/iptux_src/iptux">
<file preprocess="xml-stripblanks">gtk/AboutDialog.ui</file>
<file preprocess="xml-stripblanks">gtk/DetectPal.ui</file>
<file preprocess="xml-stripblanks">gtk/HeaderBar.ui</file>
<file preprocess="xml-stripblanks">gtk/MainWindow.ui</file>
<file preprocess="xml-stripblanks">gtk/menus.ui</file>
</gresource>
Expand Down

0 comments on commit fef69ff

Please sign in to comment.