refactor: rustfmt

This commit is contained in:
MeexReay 2025-09-03 20:16:04 +03:00
parent 431736e967
commit 5e4957a1e4
3 changed files with 102 additions and 90 deletions

View File

@ -4,39 +4,33 @@ use std::time::{Duration, SystemTime};
use chrono::Local; use chrono::Local;
use libadwaita::gdk::{BUTTON_PRIMARY, BUTTON_SECONDARY};
use libadwaita::gtk::{GestureLongPress, MenuButton, Popover};
use libadwaita::{
self as adw, Avatar, HeaderBar, ToolbarView
};
use adw::gdk::{Cursor, Display}; use adw::gdk::{Cursor, Display};
use adw::gio::MemoryInputStream; use adw::gio::MemoryInputStream;
use adw::glib::clone; use adw::glib::clone;
use adw::glib::{ use adw::glib::{self, source::timeout_add_local_once, timeout_add_local, ControlFlow};
self, source::timeout_add_local_once,
timeout_add_local,
ControlFlow,
};
use adw::prelude::*; use adw::prelude::*;
use adw::Application; use adw::Application;
use libadwaita::gdk::{BUTTON_PRIMARY, BUTTON_SECONDARY};
use libadwaita::gtk::{GestureLongPress, MenuButton, Popover};
use libadwaita::{self as adw, Avatar, HeaderBar, ToolbarView};
use adw::gtk; use adw::gtk;
use gtk::gdk_pixbuf::PixbufAnimation; use gtk::gdk_pixbuf::PixbufAnimation;
use gtk::pango::WrapMode; use gtk::pango::WrapMode;
use gtk::{ use gtk::{
Align, Box as GtkBox, Button, Calendar, Entry, Fixed, GestureClick, Justification, Label, ListBox, Align, Box as GtkBox, Button, Calendar, Entry, Fixed, GestureClick, Justification, Label,
Orientation, Overlay, Picture, ScrolledWindow, ListBox, Orientation, Overlay, Picture, ScrolledWindow,
}; };
use crate::chat::{ use crate::chat::{
config::get_config_path, config::get_config_path, ctx::Context, on_send_message, parse_message, SERVER_LIST,
ctx::Context,
on_send_message, parse_message, SERVER_LIST,
}; };
use super::widgets::CustomLayout; use super::widgets::CustomLayout;
use super::{add_chat_messages, build_menu, get_avatar_id, get_message_sign, load_pixbuf, send_notification, try_save_config, update_window_title, UiModel}; use super::{
add_chat_messages, build_menu, get_avatar_id, get_message_sign, load_pixbuf, send_notification,
try_save_config, update_window_title, UiModel,
};
pub fn get_message_box( pub fn get_message_box(
ctx: Arc<Context>, ctx: Arc<Context>,
@ -136,19 +130,22 @@ pub fn get_message_box(
fn open_avatar_popup(avatar: String, avatar_picture: &Avatar) { fn open_avatar_popup(avatar: String, avatar_picture: &Avatar) {
let display = Display::default().unwrap(); let display = Display::default().unwrap();
let clipboard = display.clipboard(); let clipboard = display.clipboard();
let popover = Popover::new(); let popover = Popover::new();
let button = Button::with_label("Copy Link"); let button = Button::with_label("Copy Link");
button.connect_clicked(clone!( button.connect_clicked(clone!(
#[weak] clipboard, #[weak]
#[weak] popover, clipboard,
#[strong] avatar, #[weak]
popover,
#[strong]
avatar,
move |_| { move |_| {
clipboard.set_text(avatar.as_str()); clipboard.set_text(avatar.as_str());
popover.popdown(); popover.popdown();
}) }
); ));
let vbox = GtkBox::builder() let vbox = GtkBox::builder()
.orientation(Orientation::Vertical) .orientation(Orientation::Vertical)
@ -166,7 +163,7 @@ pub fn get_new_message_box(
ui: &UiModel, ui: &UiModel,
message: String, message: String,
notify: bool, notify: bool,
formatting_enabled: bool formatting_enabled: bool,
) -> Overlay { ) -> Overlay {
// TODO: softcode these colors // TODO: softcode these colors
@ -193,7 +190,7 @@ pub fn get_new_message_box(
.map(|o| o.1.to_string()) .map(|o| o.1.to_string())
.unwrap_or("#DDDDDD".to_string()), .unwrap_or("#DDDDDD".to_string()),
avatar.clone(), avatar.clone(),
avatar.map(|o| get_avatar_id(&o)).unwrap_or_default() avatar.map(|o| get_avatar_id(&o)).unwrap_or_default(),
) )
} else { } else {
( (
@ -203,18 +200,18 @@ pub fn get_new_message_box(
"System".to_string(), "System".to_string(),
"#DDDDDD".to_string(), "#DDDDDD".to_string(),
None, None,
0 0,
) )
}; };
if notify && !ui.window.is_active() { if notify && !ui.window.is_active() {
if ctx.config(|o| o.chunked_enabled) { if ctx.config(|o| o.chunked_enabled) {
send_notification( send_notification(
ctx.clone(), ctx.clone(),
ui, ui,
&if name == *"System" { &if name == *"System" {
"System Message".to_string() "System Message".to_string()
} else { } else {
format!("{}'s Message", name) format!("{}'s Message", name)
}, },
&glib::markup_escape_text(&content), &glib::markup_escape_text(&content),
@ -239,20 +236,20 @@ pub fn get_new_message_box(
.show_initials(true) .show_initials(true)
.size(32) .size(32)
.build(); .build();
avatar_picture.set_vexpand(false); avatar_picture.set_vexpand(false);
avatar_picture.set_hexpand(false); avatar_picture.set_hexpand(false);
avatar_picture.set_valign(Align::Start); avatar_picture.set_valign(Align::Start);
avatar_picture.set_halign(Align::Start); avatar_picture.set_halign(Align::Start);
if let Some(avatar) = avatar { if let Some(avatar) = avatar {
let long_gesture = GestureLongPress::builder() let long_gesture = GestureLongPress::builder().button(BUTTON_PRIMARY).build();
.button(BUTTON_PRIMARY)
.build();
long_gesture.connect_pressed(clone!( long_gesture.connect_pressed(clone!(
#[weak] avatar_picture, #[weak]
#[strong] avatar, avatar_picture,
#[strong]
avatar,
move |_, x, y| { move |_, x, y| {
if x < 32.0 && y > 4.0 && y < 32.0 { if x < 32.0 && y > 4.0 && y < 32.0 {
open_avatar_popup(avatar.clone(), &avatar_picture); open_avatar_popup(avatar.clone(), &avatar_picture);
@ -261,14 +258,14 @@ pub fn get_new_message_box(
)); ));
overlay.add_controller(long_gesture); overlay.add_controller(long_gesture);
let short_gesture = GestureClick::builder() let short_gesture = GestureClick::builder().button(BUTTON_SECONDARY).build();
.button(BUTTON_SECONDARY)
.build();
short_gesture.connect_released(clone!( short_gesture.connect_released(clone!(
#[weak] avatar_picture, #[weak]
#[strong] avatar, avatar_picture,
#[strong]
avatar,
move |_, _, x, y| { move |_, _, x, y| {
if x < 32.0 && y > 4.0 && y < 32.0 { if x < 32.0 && y > 4.0 && y < 32.0 {
open_avatar_popup(avatar.clone(), &avatar_picture); open_avatar_popup(avatar.clone(), &avatar_picture);
@ -281,7 +278,7 @@ pub fn get_new_message_box(
if avatar_id != 0 { if avatar_id != 0 {
let mut lock = ui.avatars.lock().unwrap(); let mut lock = ui.avatars.lock().unwrap();
if let Some(pics) = lock.get_mut(&avatar_id) { if let Some(pics) = lock.get_mut(&avatar_id) {
pics.push(avatar_picture.clone()); pics.push(avatar_picture.clone());
} else { } else {
@ -300,7 +297,7 @@ pub fn get_new_message_box(
vbox.append(&Label::builder() vbox.append(&Label::builder()
.label(format!( .label(format!(
"<span color=\"{color}\">{}</span> <span color=\"{date_color}\">{}</span> <span color=\"{ip_color}\">{}</span>", "<span color=\"{color}\">{}</span> <span color=\"{date_color}\">{}</span> <span color=\"{ip_color}\">{}</span>",
glib::markup_escape_text(&name), glib::markup_escape_text(&name),
glib::markup_escape_text(&date), glib::markup_escape_text(&date),
glib::markup_escape_text(&ip.unwrap_or_default()), glib::markup_escape_text(&ip.unwrap_or_default()),
)) ))
@ -313,18 +310,20 @@ pub fn get_new_message_box(
.build()); .build());
} }
vbox.append(&Label::builder() vbox.append(
.label(format!( &Label::builder()
"<span color=\"{text_color}\">{}</span>", .label(format!(
glib::markup_escape_text(&content) "<span color=\"{text_color}\">{}</span>",
)) glib::markup_escape_text(&content)
.halign(Align::Start) ))
.hexpand(true) .halign(Align::Start)
.selectable(true) .hexpand(true)
.wrap(true) .selectable(true)
.wrap_mode(WrapMode::WordChar) .wrap(true)
.use_markup(true) .wrap_mode(WrapMode::WordChar)
.build()); .use_markup(true)
.build(),
);
vbox.set_margin_start(37); vbox.set_margin_start(37);
vbox.set_hexpand(true); vbox.set_hexpand(true);
@ -341,7 +340,10 @@ pub fn get_new_message_box(
} }
/// header, page_box, chat_box, chat_scrolled /// header, page_box, chat_box, chat_scrolled
pub fn build_page(ctx: Arc<Context>, app: &Application) -> (HeaderBar, GtkBox, GtkBox, ScrolledWindow) { pub fn build_page(
ctx: Arc<Context>,
app: &Application,
) -> (HeaderBar, GtkBox, GtkBox, ScrolledWindow) {
let page_box = GtkBox::new(Orientation::Vertical, 5); let page_box = GtkBox::new(Orientation::Vertical, 5);
page_box.set_css_classes(&["page-box"]); page_box.set_css_classes(&["page-box"]);
@ -349,15 +351,17 @@ pub fn build_page(ctx: Arc<Context>, app: &Application) -> (HeaderBar, GtkBox, G
let header = HeaderBar::new(); let header = HeaderBar::new();
header.pack_end(&MenuButton::builder() header.pack_end(
.icon_name("open-menu-symbolic") &MenuButton::builder()
.menu_model(&build_menu(ctx.clone(), &app)) .icon_name("open-menu-symbolic")
.build()); .menu_model(&build_menu(ctx.clone(), &app))
.build(),
);
toolbar.set_content(Some(&header)); toolbar.set_content(Some(&header));
page_box.append(&toolbar); page_box.append(&toolbar);
page_box.append(&build_widget_box(ctx.clone(), app)); page_box.append(&build_widget_box(ctx.clone(), app));
let chat_box = GtkBox::new(Orientation::Vertical, 2); let chat_box = GtkBox::new(Orientation::Vertical, 2);
@ -379,7 +383,8 @@ pub fn build_page(ctx: Arc<Context>, app: &Application) -> (HeaderBar, GtkBox, G
let chat_scrolled = chat_scrolled.downgrade(); let chat_scrolled = chat_scrolled.downgrade();
move |_| { move |_| {
if let Some(chat_scrolled) = chat_scrolled.upgrade() { if let Some(chat_scrolled) = chat_scrolled.upgrade() {
let value = chat_scrolled.vadjustment().upper() - chat_scrolled.vadjustment().page_size(); let value =
chat_scrolled.vadjustment().upper() - chat_scrolled.vadjustment().page_size();
chat_scrolled.vadjustment().set_value(value); chat_scrolled.vadjustment().set_value(value);
} }
return None; return None;
@ -388,13 +393,18 @@ pub fn build_page(ctx: Arc<Context>, app: &Application) -> (HeaderBar, GtkBox, G
page_box.set_layout_manager(Some(layout)); page_box.set_layout_manager(Some(layout));
timeout_add_local_once(Duration::ZERO, clone!( timeout_add_local_once(
#[weak] chat_scrolled, Duration::ZERO,
move || { clone!(
let value = chat_scrolled.vadjustment().upper() - chat_scrolled.vadjustment().page_size(); #[weak]
chat_scrolled.vadjustment().set_value(value); chat_scrolled,
} move || {
)); let value =
chat_scrolled.vadjustment().upper() - chat_scrolled.vadjustment().page_size();
chat_scrolled.vadjustment().set_value(value);
}
),
);
page_box.append(&chat_scrolled); page_box.append(&chat_scrolled);
@ -419,8 +429,10 @@ pub fn build_page(ctx: Arc<Context>, app: &Application) -> (HeaderBar, GtkBox, G
.build(); .build();
send_btn.connect_clicked(clone!( send_btn.connect_clicked(clone!(
#[weak] text_entry, #[weak]
#[weak] ctx, text_entry,
#[weak]
ctx,
move |_| { move |_| {
let text = text_entry.text().clone(); let text = text_entry.text().clone();
@ -444,8 +456,10 @@ pub fn build_page(ctx: Arc<Context>, app: &Application) -> (HeaderBar, GtkBox, G
)); ));
text_entry.connect_activate(clone!( text_entry.connect_activate(clone!(
#[weak] text_entry, #[weak]
#[weak] ctx, text_entry,
#[weak]
ctx,
move |_| { move |_| {
let text = text_entry.text().clone(); let text = text_entry.text().clone();
@ -477,7 +491,7 @@ pub fn build_page(ctx: Arc<Context>, app: &Application) -> (HeaderBar, GtkBox, G
fn build_widget_box(ctx: Arc<Context>, _app: &Application) -> Overlay { fn build_widget_box(ctx: Arc<Context>, _app: &Application) -> Overlay {
let widget_box_overlay = Overlay::new(); let widget_box_overlay = Overlay::new();
let widget_box = GtkBox::new(Orientation::Horizontal, 5); let widget_box = GtkBox::new(Orientation::Horizontal, 5);
widget_box.set_css_classes(&["widget-box"]); widget_box.set_css_classes(&["widget-box"]);
@ -505,7 +519,8 @@ fn build_widget_box(ctx: Arc<Context>, _app: &Application) -> Overlay {
let click = GestureClick::new(); let click = GestureClick::new();
click.connect_pressed(clone!( click.connect_pressed(clone!(
#[weak] ctx, #[weak]
ctx,
move |_, _, _, _| { move |_, _, _, _| {
let mut config = ctx.config.read().unwrap().clone(); let mut config = ctx.config.read().unwrap().clone();
config.host = url.clone(); config.host = url.clone();
@ -601,4 +616,3 @@ fn build_widget_box(ctx: Arc<Context>, _app: &Application) -> Overlay {
widget_box_overlay widget_box_overlay
} }

View File

@ -1,11 +1,11 @@
use libadwaita::{glib, gtk}; use libadwaita::{glib, gtk};
use glib::object::ObjectExt; use glib::object::ObjectExt;
use gtk::{subclass::prelude::*, prelude::LayoutManagerExt, BoxLayout}; use gtk::{prelude::LayoutManagerExt, subclass::prelude::*, BoxLayout};
#[derive(Debug)] #[derive(Debug)]
pub struct CustomLayout { pub struct CustomLayout {
box_layout: BoxLayout box_layout: BoxLayout,
} }
impl Default for CustomLayout { impl Default for CustomLayout {
@ -14,7 +14,7 @@ impl Default for CustomLayout {
box_layout: BoxLayout::builder() box_layout: BoxLayout::builder()
.orientation(gtk::Orientation::Vertical) .orientation(gtk::Orientation::Vertical)
.spacing(5) .spacing(5)
.build() .build(),
} }
} }
} }
@ -29,12 +29,10 @@ impl ObjectSubclass for CustomLayout {
impl ObjectImpl for CustomLayout { impl ObjectImpl for CustomLayout {
fn signals() -> &'static [glib::subclass::Signal] { fn signals() -> &'static [glib::subclass::Signal] {
use std::sync::OnceLock; use std::sync::OnceLock;
static SIGNALS: OnceLock<Vec<glib::subclass::Signal>> = OnceLock::new(); static SIGNALS: OnceLock<Vec<glib::subclass::Signal>> = OnceLock::new();
SIGNALS.get_or_init(|| { SIGNALS.get_or_init(|| vec![glib::subclass::Signal::builder("size-changed").build()])
vec![glib::subclass::Signal::builder("size-changed").build()]
})
} }
} }
impl LayoutManagerImpl for CustomLayout { impl LayoutManagerImpl for CustomLayout {
@ -43,11 +41,11 @@ impl LayoutManagerImpl for CustomLayout {
self.box_layout.allocate(widget, width, height, baseline) self.box_layout.allocate(widget, width, height, baseline)
} }
fn measure( fn measure(
&self, &self,
widget: &gtk::Widget, widget: &gtk::Widget,
orientation: gtk::Orientation, orientation: gtk::Orientation,
for_size: i32, for_size: i32,
) -> (i32, i32, i32, i32) { ) -> (i32, i32, i32, i32) {
self.box_layout.measure(widget, orientation, for_size) self.box_layout.measure(widget, orientation, for_size)
} }
} }

View File

@ -1,7 +1,7 @@
mod imp; mod imp;
use libadwaita::gtk::glib;
use libadwaita::gtk; use libadwaita::gtk;
use libadwaita::gtk::glib;
glib::wrapper! { glib::wrapper! {
pub struct CustomLayout(ObjectSubclass<imp::CustomLayout>) pub struct CustomLayout(ObjectSubclass<imp::CustomLayout>)