-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade to GTK 4.12. Get rid of deprecation warnings.
- Loading branch information
Showing
24 changed files
with
317 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,22 @@ | ||
use gtk::prelude::*; | ||
|
||
pub enum AskSave { | ||
Discard, | ||
Cancel, | ||
Save, | ||
} | ||
|
||
pub async fn ask_save(parent_window: >k::Window, message: &str) -> AskSave { | ||
let dlg = gtk::MessageDialog::builder() | ||
let answer = gtk::AlertDialog::builder() | ||
.modal(true) | ||
.transient_for(parent_window) | ||
.title("Password Storage") | ||
.icon_name("password-storage") | ||
.message_type(gtk::MessageType::Warning) | ||
.use_markup(false) | ||
.text(message) | ||
.build(); | ||
dlg.add_button("_Discard changes", gtk::ResponseType::Reject); | ||
dlg.add_button("_Cancel", gtk::ResponseType::Cancel); | ||
dlg.add_button("_Save", gtk::ResponseType::Ok); | ||
dlg.set_default_response(gtk::ResponseType::Ok); | ||
let answer = dlg.run_future().await; | ||
dlg.close(); | ||
.buttons(["Discard changes", "Cancel", "Save"]) | ||
.default_button(2) | ||
.cancel_button(1) | ||
.message(message) | ||
.build() | ||
.choose_future(Some(parent_window)) | ||
.await; | ||
match answer { | ||
gtk::ResponseType::Reject => AskSave::Discard, | ||
gtk::ResponseType::Ok => AskSave::Save, | ||
Ok(0) => AskSave::Discard, | ||
Ok(2) => AskSave::Save, | ||
_ => AskSave::Cancel, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,24 @@ | ||
use gtk::prelude::*; | ||
use std::path::PathBuf; | ||
|
||
async fn choose_file( | ||
action: gtk::FileChooserAction, | ||
title: &str, | ||
parent_window: >k::Window, | ||
) -> Option<PathBuf> { | ||
let dlg = gtk::FileChooserNative::builder() | ||
.modal(true) | ||
.action(action) | ||
.title(title) | ||
.transient_for(parent_window) | ||
.build(); | ||
let answer = dlg.run_future().await; | ||
dlg.hide(); | ||
if answer == gtk::ResponseType::Accept { | ||
dlg.file().and_then(|f| f.path()) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
pub async fn open_file(parent_window: >k::Window) -> Option<PathBuf> { | ||
choose_file(gtk::FileChooserAction::Open, "Open file", parent_window).await | ||
gtk::FileDialog::builder() | ||
.modal(true) | ||
.title("Open file") | ||
.build() | ||
.open_future(Some(parent_window)) | ||
.await | ||
.ok() | ||
.and_then(|f| f.path()) | ||
} | ||
|
||
pub async fn save_file(parent_window: >k::Window) -> Option<PathBuf> { | ||
choose_file(gtk::FileChooserAction::Save, "Save file", parent_window).await | ||
gtk::FileDialog::builder() | ||
.modal(true) | ||
.title("Save file") | ||
.build() | ||
.save_future(Some(parent_window)) | ||
.await | ||
.ok() | ||
.and_then(|f| f.path()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
use crate::compat::accel::PRIMARY_MODIFIER; | ||
use gtk::{gdk, glib, prelude::*, subclass::prelude::*}; | ||
|
||
mod imp { | ||
use super::*; | ||
use crate::utils::ui::title; | ||
use futures::channel::mpsc::{channel, Receiver, Sender}; | ||
use futures::stream::StreamExt; | ||
use std::cell::RefCell; | ||
|
||
pub struct GenericDialog { | ||
pub title: gtk::Label, | ||
pub cancel_button: gtk::Button, | ||
pub ok_button: gtk::Button, | ||
pub sender: RefCell<Option<Sender<gtk::ResponseType>>>, | ||
pub receiver: RefCell<Option<Receiver<gtk::ResponseType>>>, | ||
} | ||
|
||
#[glib::object_subclass] | ||
impl ObjectSubclass for GenericDialog { | ||
const NAME: &'static str = "PSGenericDialog"; | ||
type Type = super::GenericDialog; | ||
type ParentType = gtk::Window; | ||
|
||
fn new() -> Self { | ||
let title = title(""); | ||
|
||
let cancel_button = gtk::Button::builder().label("Cancel").build(); | ||
|
||
let ok_button = gtk::Button::builder() | ||
.label("OK") | ||
.receives_default(true) | ||
.build(); | ||
ok_button.add_css_class("suggested-action"); | ||
|
||
Self { | ||
title, | ||
cancel_button, | ||
ok_button, | ||
sender: Default::default(), | ||
receiver: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
impl ObjectImpl for GenericDialog { | ||
fn constructed(&self) { | ||
self.parent_constructed(); | ||
|
||
let header = gtk::HeaderBar::builder() | ||
.title_widget(&self.title) | ||
.show_title_buttons(false) | ||
.build(); | ||
|
||
let (sender, receiver) = channel::<gtk::ResponseType>(0); | ||
*self.sender.borrow_mut() = Some(sender.clone()); | ||
*self.receiver.borrow_mut() = Some(receiver); | ||
|
||
self.cancel_button.connect_clicked( | ||
glib::clone!(@weak self as imp => move |_| imp.send(gtk::ResponseType::Cancel)), | ||
); | ||
header.pack_start(&self.cancel_button); | ||
|
||
self.ok_button.connect_clicked( | ||
glib::clone!(@weak self as imp => move |_| imp.send(gtk::ResponseType::Ok)), | ||
); | ||
header.pack_end(&self.ok_button); | ||
|
||
self.obj().set_modal(true); | ||
self.obj().set_resizable(true); | ||
self.obj().set_titlebar(Some(&header)); | ||
self.obj().set_icon_name(Some("password-storage")); | ||
|
||
let key_controller = gtk::EventControllerKey::new(); | ||
key_controller.connect_key_pressed( | ||
glib::clone!(@weak self as imp => @default-return glib::Propagation::Proceed, move |_controller, key, _keycode, modifier| { | ||
const NO_MODIFIER: gdk::ModifierType = gdk::ModifierType::empty(); | ||
match (key, modifier) { | ||
(gdk::Key::Escape, NO_MODIFIER) | ||
| (gdk::Key::w, PRIMARY_MODIFIER) | ||
| (gdk::Key::W, PRIMARY_MODIFIER) => { | ||
imp.send(gtk::ResponseType::Cancel); | ||
glib::Propagation::Stop | ||
} | ||
(gdk::Key::Return, NO_MODIFIER) => { | ||
imp.ok_button.activate(); | ||
glib::Propagation::Stop | ||
} | ||
_ => glib::Propagation::Proceed, | ||
} | ||
}), | ||
); | ||
self.obj().add_controller(key_controller); | ||
} | ||
} | ||
|
||
impl WidgetImpl for GenericDialog {} | ||
impl WindowImpl for GenericDialog {} | ||
|
||
impl GenericDialog { | ||
pub fn send(&self, response: gtk::ResponseType) { | ||
let mut sender_opt = self.sender.borrow_mut(); | ||
let Some(sender) = sender_opt.as_mut() else { | ||
eprintln!("No sender"); | ||
return; | ||
}; | ||
if let Err(err) = sender.try_send(response) { | ||
eprintln!("Cannot send response {}. {}", response, err); | ||
} | ||
} | ||
|
||
pub async fn next_response(&self) -> Option<gtk::ResponseType> { | ||
self.receiver.borrow_mut().as_mut()?.next().await | ||
} | ||
} | ||
} | ||
|
||
glib::wrapper! { | ||
pub struct GenericDialog(ObjectSubclass<imp::GenericDialog>) | ||
@extends gtk::Widget, gtk::Window; | ||
} | ||
|
||
impl Default for GenericDialog { | ||
fn default() -> Self { | ||
glib::Object::builder().build() | ||
} | ||
} | ||
|
||
impl GenericDialog { | ||
pub fn set_title(&self, title: &str) { | ||
self.imp().title.set_label(title); | ||
} | ||
|
||
pub fn set_ok_label(&self, label: &str) { | ||
self.imp().ok_button.set_label(label); | ||
} | ||
|
||
pub fn set_ok_sensitive(&self, sensitive: bool) { | ||
self.imp().ok_button.set_sensitive(sensitive); | ||
} | ||
|
||
pub fn emit_response(&self, response: gtk::ResponseType) { | ||
self.imp().send(response); | ||
} | ||
|
||
pub async fn run(&self) -> Option<gtk::ResponseType> { | ||
self.present(); | ||
let result = self.imp().next_response().await; | ||
self.set_visible(false); | ||
result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
pub mod about; | ||
pub mod ask; | ||
pub mod ask_save; | ||
pub mod change_password; | ||
pub mod file_chooser; | ||
pub mod generic_dialog; | ||
pub mod preferences; | ||
pub mod say; | ||
pub mod shortcuts; | ||
pub mod show_uri; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.