-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(message-document): Use mask for status indicator
With mask it correctly matches message bubble background
- Loading branch information
Showing
6 changed files
with
187 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use tdlib::types::File; | ||
use FileStatus::*; | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq)] | ||
pub(crate) enum FileStatus { | ||
Downloading(f64), | ||
Uploading(f64), | ||
CanBeDownloaded, | ||
Downloaded, | ||
} | ||
|
||
impl From<&File> for FileStatus { | ||
fn from(file: &File) -> Self { | ||
let local = &file.local; | ||
let remote = &file.remote; | ||
|
||
let size = file.size.max(file.expected_size) as u64; | ||
|
||
if local.is_downloading_active { | ||
let progress = local.downloaded_size as f64 / size as f64; | ||
Downloading(progress) | ||
} else if remote.is_uploading_active { | ||
let progress = remote.uploaded_size as f64 / size as f64; | ||
Uploading(progress) | ||
} else if local.is_downloading_completed { | ||
Downloaded | ||
} else if local.can_be_downloaded { | ||
CanBeDownloaded | ||
} else { | ||
dbg!(file); | ||
unimplemented!("unknown file status"); | ||
} | ||
} | ||
} |
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
117 changes: 117 additions & 0 deletions
117
src/session/content/message_row/document/status_indicator.rs
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,117 @@ | ||
use std::cell::Cell; | ||
|
||
use adw::prelude::*; | ||
use adw::subclass::prelude::*; | ||
use gtk::glib; | ||
use gtk::gsk; | ||
use gtk::CompositeTemplate; | ||
|
||
use super::file_status::FileStatus; | ||
|
||
mod imp { | ||
use super::*; | ||
|
||
#[derive(Debug, Default, CompositeTemplate, glib::Properties)] | ||
#[template(string = r#" | ||
template $MessageDocumentStatusIndicator { | ||
layout-manager: BinLayout {}; | ||
overflow: hidden; | ||
Image status_image {} | ||
} | ||
"#)] | ||
#[properties(wrapper_type = super::StatusIndicator)] | ||
pub(crate) struct StatusIndicator { | ||
#[property(get, set = Self::set_masked, explicit_notify)] | ||
pub(super) masked: Cell<bool>, | ||
#[template_child] | ||
pub(super) status_image: TemplateChild<gtk::Image>, | ||
} | ||
|
||
#[glib::object_subclass] | ||
impl ObjectSubclass for StatusIndicator { | ||
const NAME: &'static str = "MessageDocumentStatusIndicator"; | ||
type Type = super::StatusIndicator; | ||
type ParentType = gtk::Widget; | ||
|
||
fn class_init(klass: &mut Self::Class) { | ||
klass.set_css_name("statusindicator"); | ||
klass.bind_template(); | ||
} | ||
|
||
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) { | ||
obj.init_template(); | ||
} | ||
} | ||
|
||
impl ObjectImpl for StatusIndicator { | ||
fn properties() -> &'static [glib::ParamSpec] { | ||
Self::derived_properties() | ||
} | ||
|
||
fn set_property(&self, id: usize, value: &glib::Value, pspec: &glib::ParamSpec) { | ||
self.derived_set_property(id, value, pspec) | ||
} | ||
|
||
fn property(&self, id: usize, pspec: &glib::ParamSpec) -> glib::Value { | ||
self.derived_property(id, pspec) | ||
} | ||
} | ||
|
||
impl WidgetImpl for StatusIndicator { | ||
fn snapshot(&self, snapshot: >k::Snapshot) { | ||
if self.masked.get() { | ||
let widget = self.obj(); | ||
|
||
snapshot.push_mask(gsk::MaskMode::InvertedAlpha); | ||
|
||
self.parent_snapshot(snapshot); | ||
|
||
snapshot.pop(); | ||
|
||
snapshot.append_color( | ||
&widget.color(), | ||
>k::graphene::Rect::new( | ||
0.0, | ||
0.0, | ||
widget.width() as f32, | ||
widget.height() as f32, | ||
), | ||
); | ||
|
||
snapshot.pop(); | ||
} else { | ||
self.parent_snapshot(snapshot); | ||
} | ||
} | ||
} | ||
|
||
impl BinImpl for StatusIndicator {} | ||
|
||
impl StatusIndicator { | ||
fn set_masked(&self, masked: bool) { | ||
if self.masked.replace(masked) != masked { | ||
let obj = self.obj(); | ||
obj.queue_draw(); | ||
obj.notify("masked") | ||
} | ||
} | ||
} | ||
} | ||
|
||
glib::wrapper! { | ||
pub(crate) struct StatusIndicator(ObjectSubclass<imp::StatusIndicator>) | ||
@extends gtk::Widget; | ||
} | ||
|
||
impl StatusIndicator { | ||
pub(crate) fn set_status(&self, status: FileStatus) { | ||
let icon_name = match status { | ||
FileStatus::Downloading(_) | FileStatus::Uploading(_) => "media-playback-stop-symbolic", | ||
FileStatus::CanBeDownloaded => "document-save-symbolic", | ||
FileStatus::Downloaded => "folder-documents-symbolic", | ||
}; | ||
|
||
self.imp().status_image.set_icon_name(Some(icon_name)); | ||
} | ||
} |