finish port to libadwaita dialogs

This commit is contained in:
ranfdev
2024-02-13 18:55:20 +01:00
parent 29377dc1a6
commit 86d1472a24
7 changed files with 34 additions and 59 deletions

View File

@ -1,7 +1,7 @@
using Gtk 4.0; using Gtk 4.0;
using Adw 1; using Adw 1;
template $NotifyPreferences : Adw.PreferencesWindow { template $NotifyPreferences : Adw.PreferencesDialog {
width-request: 240; width-request: 240;
height-request: 360; height-request: 360;
Adw.PreferencesPage { Adw.PreferencesPage {

View File

@ -4,13 +4,13 @@ use std::path::PathBuf;
use std::pin::Pin; use std::pin::Pin;
use std::rc::Rc; use std::rc::Rc;
use adw::prelude::*;
use adw::subclass::prelude::*; use adw::subclass::prelude::*;
use capnp_rpc::{rpc_twoparty_capnp, twoparty, RpcSystem}; use capnp_rpc::{rpc_twoparty_capnp, twoparty, RpcSystem};
use futures::stream::Stream; use futures::stream::Stream;
use futures::AsyncReadExt; use futures::AsyncReadExt;
use gio::SocketClient; use gio::SocketClient;
use gio::UnixSocketAddress; use gio::UnixSocketAddress;
use gtk::prelude::*;
use gtk::{gdk, gio, glib}; use gtk::{gdk, gio, glib};
use ntfy_daemon::models; use ntfy_daemon::models;
use ntfy_daemon::ntfy_capnp::system_notifier; use ntfy_daemon::ntfy_capnp::system_notifier;
@ -217,23 +217,20 @@ impl NotifyApplication {
} }
fn show_about_dialog(&self) { fn show_about_dialog(&self) {
let dialog = adw::AboutWindow::from_appdata( let dialog = adw::AboutDialog::from_appdata(
"/com/ranfdev/Notify/com.ranfdev.Notify.metainfo.xml", "/com/ranfdev/Notify/com.ranfdev.Notify.metainfo.xml",
None, None,
); );
if let Some(w) = self.imp().window.borrow().upgrade() { if let Some(w) = self.imp().window.borrow().upgrade() {
dialog.set_transient_for(Some(&w)); dialog.present(&w);
} }
dialog.present();
} }
fn show_preferences(&self) { fn show_preferences(&self) {
let win = crate::widgets::NotifyPreferences::new( let win = crate::widgets::NotifyPreferences::new(
self.main_window().imp().notifier.get().unwrap().clone(), self.main_window().imp().notifier.get().unwrap().clone(),
); );
win.set_transient_for(Some(&self.main_window())); win.present(&self.main_window());
win.present();
} }
pub fn run(&self) -> glib::ExitCode { pub fn run(&self) -> glib::ExitCode {

View File

@ -31,11 +31,6 @@ mod imp {
type ParentType = adw::Dialog; type ParentType = adw::Dialog;
fn class_init(klass: &mut Self::Class) { fn class_init(klass: &mut Self::Class) {
klass.add_binding_action(
gtk::gdk::Key::Escape,
gtk::gdk::ModifierType::empty(),
"window.close",
);
klass.install_action("default.activate", None, |this, _, _| { klass.install_action("default.activate", None, |this, _, _| {
this.emit_subscribe_request(); this.emit_subscribe_request();
}); });

View File

@ -20,30 +20,22 @@ mod imp {
impl ObjectSubclass for AdvancedMessageDialog { impl ObjectSubclass for AdvancedMessageDialog {
const NAME: &'static str = "AdvancedMessageDialog"; const NAME: &'static str = "AdvancedMessageDialog";
type Type = super::AdvancedMessageDialog; type Type = super::AdvancedMessageDialog;
type ParentType = adw::Window; type ParentType = adw::Dialog;
} }
impl ObjectImpl for AdvancedMessageDialog {} impl ObjectImpl for AdvancedMessageDialog {}
impl WidgetImpl for AdvancedMessageDialog {} impl WidgetImpl for AdvancedMessageDialog {}
impl WindowImpl for AdvancedMessageDialog {} impl AdwDialogImpl for AdvancedMessageDialog {}
impl AdwWindowImpl for AdvancedMessageDialog {}
} }
glib::wrapper! { glib::wrapper! {
pub struct AdvancedMessageDialog(ObjectSubclass<imp::AdvancedMessageDialog>) pub struct AdvancedMessageDialog(ObjectSubclass<imp::AdvancedMessageDialog>)
@extends gtk::Widget, gtk::Window, adw::Window; @extends gtk::Widget, adw::Dialog;
} }
impl AdvancedMessageDialog { impl AdvancedMessageDialog {
pub fn new( pub fn new(subscription: Subscription, message: String) -> Self {
parent: &impl IsA<gtk::Window>,
subscription: Subscription,
message: String,
) -> Self {
let this: Self = glib::Object::new(); let this: Self = glib::Object::new();
this.set_transient_for(Some(parent));
this.set_modal(true);
this.set_default_height(400);
this.imp().subscription.set(subscription).unwrap(); this.imp().subscription.set(subscription).unwrap();
this.build_ui( this.build_ui(
this.imp().subscription.get().unwrap().topic().clone(), this.imp().subscription.get().unwrap().topic().clone(),
@ -52,6 +44,9 @@ impl AdvancedMessageDialog {
this this
} }
fn build_ui(&self, topic: String, message: String) { fn build_ui(&self, topic: String, message: String) {
self.set_title("Advanced Message");
self.set_content_height(480);
self.set_content_width(480);
let this = self.clone(); let this = self.clone();
relm4_macros::view! { relm4_macros::view! {
content = &adw::ToolbarView { content = &adw::ToolbarView {
@ -59,7 +54,7 @@ impl AdvancedMessageDialog {
#[wrap(Some)] #[wrap(Some)]
set_content: toast_overlay = &adw::ToastOverlay { set_content: toast_overlay = &adw::ToastOverlay {
#[wrap(Some)] #[wrap(Some)]
set_child = &adw::Clamp { set_child = &gtk::ScrolledWindow {
#[wrap(Some)] #[wrap(Some)]
set_child = &gtk::Box { set_child = &gtk::Box {
set_margin_top: 8, set_margin_top: 8,
@ -82,22 +77,19 @@ impl AdvancedMessageDialog {
set_xalign: 0.0, set_xalign: 0.0,
set_halign: gtk::Align::Start, set_halign: gtk::Align::Start,
}, },
append = &gtk::ScrolledWindow { append: text_view = &gsv::View {
#[wrap(Some)] add_css_class: "code",
set_child: text_view = &gsv::View { set_tab_width: 4,
add_css_class: "code", set_indent_width: 2,
set_tab_width: 4, set_auto_indent: true,
set_indent_width: 2, set_top_margin: 4,
set_auto_indent: true, set_bottom_margin: 4,
set_top_margin: 4, set_left_margin: 4,
set_bottom_margin: 4, set_right_margin: 4,
set_left_margin: 4, set_hexpand: true,
set_right_margin: 4, set_vexpand: true,
set_hexpand: true, set_monospace: true,
set_vexpand: true, set_background_pattern: gsv::BackgroundPatternType::Grid
set_monospace: true,
set_background_pattern: gsv::BackgroundPatternType::Grid
},
}, },
append = &gtk::Label { append = &gtk::Label {
add_css_class: "heading", add_css_class: "heading",
@ -166,9 +158,9 @@ impl AdvancedMessageDialog {
add_css_class: "circular", add_css_class: "circular",
add_css_class: "small", add_css_class: "small",
set_label: "?", set_label: "?",
connect_clicked[this] => move |_| { connect_clicked => move |_| {
gtk::UriLauncher::new("https://docs.ntfy.sh/publish/#publish-as-json").launch( gtk::UriLauncher::new("https://docs.ntfy.sh/publish/#publish-as-json").launch(
Some(&this), None::<&gtk::Window>,
gio::Cancellable::NONE, gio::Cancellable::NONE,
|_| {} |_| {}
); );
@ -221,6 +213,6 @@ impl AdvancedMessageDialog {
}; };
let scheme = gsv::StyleSchemeManager::default().scheme(scheme_name); let scheme = gsv::StyleSchemeManager::default().scheme(scheme_name);
buffer.set_style_scheme(scheme.as_ref()); buffer.set_style_scheme(scheme.as_ref());
this.set_content(Some(&content)); this.set_child(Some(&content));
} }
} }

View File

@ -48,7 +48,7 @@ mod imp {
impl ObjectSubclass for NotifyPreferences { impl ObjectSubclass for NotifyPreferences {
const NAME: &'static str = "NotifyPreferences"; const NAME: &'static str = "NotifyPreferences";
type Type = super::NotifyPreferences; type Type = super::NotifyPreferences;
type ParentType = adw::PreferencesWindow; type ParentType = adw::PreferencesDialog;
fn class_init(klass: &mut Self::Class) { fn class_init(klass: &mut Self::Class) {
klass.bind_template(); klass.bind_template();
@ -66,16 +66,13 @@ mod imp {
} }
impl WidgetImpl for NotifyPreferences {} impl WidgetImpl for NotifyPreferences {}
impl WindowImpl for NotifyPreferences {} impl AdwDialogImpl for NotifyPreferences {}
impl PreferencesDialogImpl for NotifyPreferences {}
impl ApplicationWindowImpl for NotifyPreferences {}
impl AdwWindowImpl for NotifyPreferences {}
impl PreferencesWindowImpl for NotifyPreferences {}
} }
glib::wrapper! { glib::wrapper! {
pub struct NotifyPreferences(ObjectSubclass<imp::NotifyPreferences>) pub struct NotifyPreferences(ObjectSubclass<imp::NotifyPreferences>)
@extends gtk::Widget, gtk::Window, adw::Window, adw::PreferencesWindow, @extends gtk::Widget, adw::Dialog, adw::PreferencesDialog,
@implements gio::ActionMap, gio::ActionGroup, gtk::Root; @implements gio::ActionMap, gio::ActionGroup, gtk::Root;
} }

View File

@ -30,11 +30,6 @@ mod imp {
fn class_init(klass: &mut Self::Class) { fn class_init(klass: &mut Self::Class) {
klass.bind_template(); klass.bind_template();
klass.add_binding_action(
gtk::gdk::Key::Escape,
gtk::gdk::ModifierType::empty(),
"window.close",
);
} }
// You must call `Widget`'s `init_template()` within `instance_init()`. // You must call `Widget`'s `init_template()` within `instance_init()`.

View File

@ -236,8 +236,7 @@ impl NotifyWindow {
imp.code_btn.connect_clicked(move |_| { imp.code_btn.connect_clicked(move |_| {
let this = this.clone(); let this = this.clone();
this.selected_subscription().map(move |sub| { this.selected_subscription().map(move |sub| {
AdvancedMessageDialog::new(&this, sub, this.imp().entry.text().to_string()) AdvancedMessageDialog::new(sub, this.imp().entry.text().to_string()).present(&this)
.present()
}); });
}); });
} }