Browse Source

attachment-dialog: Switch to AdwDialog

merge-requests/1461/merge
Kévin Commaille 2 years ago committed by Kévin Commaille
parent
commit
68f36c1f4d
  1. 29
      src/session/view/content/room_history/message_toolbar/attachment_dialog.rs
  2. 16
      src/session/view/content/room_history/message_toolbar/attachment_dialog.ui
  3. 20
      src/session/view/content/room_history/message_toolbar/mod.rs

29
src/session/view/content/room_history/message_toolbar/attachment_dialog.rs

@ -1,6 +1,6 @@
use adw::subclass::prelude::*;
use adw::{prelude::*, subclass::prelude::*};
use futures_channel::oneshot;
use gtk::{gdk, gio, glib, prelude::*, CompositeTemplate};
use gtk::{gdk, gio, glib, CompositeTemplate};
use tracing::error;
use crate::components::MediaContentViewer;
@ -28,13 +28,11 @@ mod imp {
impl ObjectSubclass for AttachmentDialog {
const NAME: &'static str = "AttachmentDialog";
type Type = super::AttachmentDialog;
type ParentType = adw::Window;
type ParentType = adw::Dialog;
fn class_init(klass: &mut Self::Class) {
Self::bind_template(klass);
Self::Type::bind_template_callbacks(klass);
klass.add_binding_action(gdk::Key::Escape, gdk::ModifierType::empty(), "window.close");
}
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
@ -52,16 +50,12 @@ mod imp {
impl WidgetImpl for AttachmentDialog {}
impl WindowImpl for AttachmentDialog {
fn close_request(&self) -> glib::Propagation {
impl AdwDialogImpl for AttachmentDialog {
fn closed(&self) {
self.send_response(gtk::ResponseType::Cancel);
glib::Propagation::Proceed
}
}
impl AdwWindowImpl for AttachmentDialog {}
impl AttachmentDialog {
/// Set whether this dialog is loading.
pub(super) fn set_loading(&self, loading: bool) {
@ -88,7 +82,7 @@ mod imp {
glib::wrapper! {
/// A dialog to preview an attachment before sending it.
pub struct AttachmentDialog(ObjectSubclass<imp::AttachmentDialog>)
@extends gtk::Widget, gtk::Window, gtk::Root, adw::Window;
@extends gtk::Widget, adw::Dialog;
}
#[gtk::template_callbacks]
@ -96,11 +90,8 @@ impl AttachmentDialog {
/// Create an attachment dialog with the given title.
///
/// Its initial state is loading.
pub fn new(transient_for: &gtk::Window, title: &str) -> Self {
glib::Object::builder()
.property("transient-for", transient_for)
.property("title", title)
.build()
pub fn new(title: &str) -> Self {
glib::Object::builder().property("title", title).build()
}
/// Set the image to preview.
@ -135,11 +126,11 @@ impl AttachmentDialog {
///
/// The response is [`gtk::ResponseType::Ok`] if the user clicked on send,
/// otherwise it is [`gtk::ResponseType::Cancel`].
pub async fn response_future(&self) -> gtk::ResponseType {
pub async fn response_future(&self, parent: &impl IsA<gtk::Widget>) -> gtk::ResponseType {
let (sender, receiver) = oneshot::channel();
self.imp().sender.replace(Some(sender));
self.present();
self.present(parent);
receiver.await.unwrap_or(gtk::ResponseType::Cancel)
}

16
src/session/view/content/room_history/message_toolbar/attachment_dialog.ui

@ -1,16 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="AttachmentDialog" parent="AdwWindow">
<property name="modal">True</property>
<property name="title"></property>
<property name="default-width">400</property>
<property name="default-height">400</property>
<property name="destroy-with-parent">True</property>
<property name="content">
<template class="AttachmentDialog" parent="AdwDialog">
<property name="content-width">400</property>
<property name="content-height">400</property>
<property name="child">
<object class="AdwToolbarView">
<child type="top">
<object class="GtkHeaderBar">
<property name="show-title-buttons">False</property>
<object class="AdwHeaderBar">
<property name="show-start-title-buttons">False</property>
<property name="show-end-title-buttons">False</property>
<child>
<object class="GtkButton" id="cancel_button">
<property name="label" translatable="yes">_Cancel</property>

20
src/session/view/content/room_history/message_toolbar/mod.rs

@ -1,10 +1,9 @@
use adw::subclass::prelude::*;
use adw::{prelude::*, subclass::prelude::*};
use futures_util::{future, pin_mut, StreamExt};
use gettextrs::{gettext, pgettext};
use gtk::{
gdk, gio,
glib::{self, clone},
prelude::*,
CompositeTemplate,
};
use matrix_sdk::{
@ -635,9 +634,6 @@ impl MessageToolbar {
let Some(room) = self.room() else {
return;
};
let Some(window) = self.root().and_downcast::<gtk::Window>() else {
return;
};
let location = Location::new();
if !location.is_available() {
@ -645,8 +641,8 @@ impl MessageToolbar {
}
// Show the dialog as loading first.
let dialog = AttachmentDialog::new(&window, &gettext("Your Location"));
let response_fut = dialog.response_future();
let dialog = AttachmentDialog::new(&gettext("Your Location"));
let response_fut = dialog.response_future(self);
pin_mut!(response_fut);
// Listen whether the user cancels before the location API is initialized.
@ -756,12 +752,11 @@ impl MessageToolbar {
return;
}
let window = self.root().and_downcast::<gtk::Window>().unwrap();
let filename = filename_for_mime(Some(mime::IMAGE_PNG.as_ref()), None);
let dialog = AttachmentDialog::new(&window, &filename);
let dialog = AttachmentDialog::new(&filename);
dialog.set_image(&image);
if dialog.response_future().await != gtk::ResponseType::Ok {
if dialog.response_future(self).await != gtk::ResponseType::Ok {
return;
}
@ -816,11 +811,10 @@ impl MessageToolbar {
match load_file(&file).await {
Ok((bytes, file_info)) => {
let window = self.root().and_downcast::<gtk::Window>().unwrap();
let dialog = AttachmentDialog::new(&window, &file_info.filename);
let dialog = AttachmentDialog::new(&file_info.filename);
dialog.set_file(&file);
if dialog.response_future().await != gtk::ResponseType::Ok {
if dialog.response_future(self).await != gtk::ResponseType::Ok {
return;
}

Loading…
Cancel
Save