application: Move some actions to the main window

They are only useful if there is a main window.
This commit is contained in:
Kévin Commaille 2023-11-17 16:27:32 +01:00 committed by Kévin Commaille
parent de6c8af2f3
commit fd37835049
4 changed files with 31 additions and 54 deletions

View file

@ -19,7 +19,7 @@
</child>
<child>
<object class="GtkButton">
<property name="action-name">app.new-session</property>
<property name="action-name">win.new-session</property>
<property name="child">
<object class="GtkBox">
<property name="spacing">10</property>

View file

@ -2,7 +2,6 @@ use std::{borrow::Cow, fmt};
use gettextrs::gettext;
use gio::{ApplicationFlags, Settings};
use glib::{clone, WeakRef};
use gtk::{gio, glib, prelude::*, subclass::prelude::*};
use ruma::{OwnedRoomId, RoomId};
use tracing::{debug, info};
@ -16,7 +15,7 @@ mod imp {
#[derive(Debug)]
pub struct Application {
pub window: WeakRef<Window>,
pub window: glib::WeakRef<Window>,
pub settings: Settings,
}
@ -54,19 +53,6 @@ mod imp {
app.setup_gactions();
app.setup_accels();
let monitor = gio::NetworkMonitor::default();
monitor.connect_network_changed(clone!(@weak app => move |monitor, _| {
app.lookup_action("show-login")
.and_downcast::<gio::SimpleAction>()
.unwrap()
.set_enabled(monitor.is_network_available());
}));
app.lookup_action("show-login")
.and_downcast::<gio::SimpleAction>()
.unwrap()
.set_enabled(monitor.is_network_available());
app.main_window().present();
}
@ -119,16 +105,6 @@ impl Application {
app.show_about_dialog();
})
.build(),
gio::ActionEntry::builder("new-session")
.activate(|app: &Application, _, _| {
app.main_window().switch_to_greeter_page();
})
.build(),
gio::ActionEntry::builder("show-login")
.activate(|app: &Application, _, _| {
app.main_window().switch_to_login_page();
})
.build(),
gio::ActionEntry::builder("show-room")
.parameter_type(Some(&AppShowRoomPayload::static_variant_type()))
.activate(|app: &Application, _, v| {
@ -139,21 +115,6 @@ impl Application {
})
.build(),
]);
let show_session_action = gio::SimpleAction::new("show-session", None);
show_session_action.connect_activate(clone!(@weak self as app => move |_, _| {
app.main_window().switch_to_session_page();
}));
self.add_action(&show_session_action);
let win = self.main_window();
let session_list = win.session_list();
session_list.connect_is_empty_notify(
clone!(@weak show_session_action => move |session_list| {
show_session_action.set_enabled(!session_list.is_empty());
}),
);
show_session_action.set_enabled(!session_list.is_empty());
}
/// Sets up keyboard shortcuts for application and window actions.

View file

@ -7,7 +7,7 @@
<object class="AdwHeaderBar">
<child type="start">
<object class="GtkButton" id="back_button">
<property name="action-name">app.show-session</property>
<property name="action-name">win.show-session</property>
<property name="visible" bind-source="back_button" bind-property="sensitive" bind-flags="sync-create"/>
<property name="icon-name">go-previous-symbolic</property>
<property name="tooltip-text" translatable="yes">Back</property>
@ -113,7 +113,7 @@
<property name="can-shrink">true</property>
<property name="label" translatable="yes">_Log In</property>
<property name="use-underline">true</property>
<property name="action-name">app.show-login</property>
<property name="action-name">win.show-login</property>
</object>
</child>
<child>

View file

@ -95,6 +95,24 @@ mod imp {
}
},
);
klass.install_action("win.new-session", None, |obj, _, _| {
obj.switch_to_greeter_page();
});
klass.install_action("win.show-login", None, |obj, _, _| {
obj.switch_to_login_page();
});
klass.install_action("win.show-session", None, |obj, _, _| {
obj.switch_to_session_page();
});
klass.install_action("win.toggle-fullscreen", None, |obj, _, _| {
if obj.is_fullscreened() {
obj.unfullscreen();
} else {
obj.fullscreen();
}
});
}
fn instance_init(obj: &InitializingObject<Self>) {
@ -156,16 +174,11 @@ mod imp {
obj.load_window_size();
// Ask for the toggle fullscreen state
let fullscreen = gio::SimpleAction::new("toggle-fullscreen", None);
fullscreen.connect_activate(clone!(@weak obj as window => move |_, _| {
if window.is_fullscreened() {
window.unfullscreen();
} else {
window.fullscreen();
}
}));
obj.add_action(&fullscreen);
self.session_list
.connect_is_empty_notify(clone!(@weak obj => move |session_list| {
obj.action_set_enabled("win.show-session", !session_list.is_empty());
}));
obj.action_set_enabled("win.show-session", !self.session_list.is_empty());
self.main_stack.connect_visible_child_notify(
clone!(@weak obj => move |_| obj.set_default_by_child()),
@ -492,7 +505,10 @@ impl Window {
let imp = self.imp();
let monitor = gio::NetworkMonitor::default();
if !monitor.is_network_available() {
let is_network_available = monitor.is_network_available();
self.action_set_enabled("win.show-login", is_network_available);
if !is_network_available {
imp.offline_banner
.set_title(&gettext("No network connection"));
imp.offline_banner.set_revealed(true);