From e056cb9fec031573f8b99bc4f1846ee530364b07 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Mon, 2 Mar 2026 14:25:04 -0800 Subject: [PATCH] fix clippy warnings --- src/app.rs | 130 ++++++++++++++++++++++++++++------------------------- 1 file changed, 70 insertions(+), 60 deletions(-) diff --git a/src/app.rs b/src/app.rs index 4970135..2769405 100644 --- a/src/app.rs +++ b/src/app.rs @@ -3,7 +3,9 @@ use cosmic::iced::{Color, Font, Limits, Rectangle, Subscription, window}; use cosmic::iced::alignment::{Horizontal, Vertical}; use cosmic::iced::platform_specific::shell::wayland::commands::popup::{destroy_popup, get_popup}; -use cosmic::widget::{self, autosize, container, rectangle_tracker::*}; +use cosmic::widget::{self, autosize, container, rectangle_tracker::{ + RectangleTracker, RectangleUpdate, rectangle_tracker_subscription, +}}; use cosmic::{iced_futures, prelude::*, surface}; use cosmic::applet::cosmic_panel_config::PanelAnchor; use cosmic::iced_core::Shadow; @@ -144,6 +146,7 @@ impl cosmic::Application for AppModel { // Calculate the width needed for the monospace content. // The default monospace font is ~8.4px per character at 14px font size, // plus padding for container (12*2) and popup chrome (~32). + #[allow(clippy::cast_precision_loss)] let max_line_len = self.full_weather.lines() .map(|line| line.chars().count()) .max() @@ -236,7 +239,7 @@ impl cosmic::Application for AppModel { self.last_updated = format_current_time(); } Err(e) => { - eprintln!("Weather fetch error: {}", e); + eprintln!("Weather fetch error: {e}"); self.weather_text = if e.contains("network") || e.contains("timeout") { "🌐❌".to_string() // Network issue } else { @@ -252,7 +255,7 @@ impl cosmic::Application for AppModel { self.full_weather = weather; } Err(e) => { - eprintln!("Full weather fetch error: {}", e); + eprintln!("Full weather fetch error: {e}"); self.full_weather = "Failed to load weather".to_string(); } } @@ -263,61 +266,7 @@ impl cosmic::Application for AppModel { cosmic::Action::App(Message::WeatherUpdate(result)) }) } - Message::TogglePopup => { - if let Some(p) = self.popup.take() { - destroy_popup(p) - } else { - let new_id = window::Id::unique(); - self.popup = Some(new_id); - - let mut popup_settings = self.core.applet.get_popup_settings( - self.core.main_window_id().unwrap(), - new_id, - None, - None, - None, - ); - - let Rectangle { x, y, width, height } = self.rectangle; - popup_settings.positioner.anchor_rect = Rectangle:: { - x: x.max(1.) as i32, - y: y.max(1.) as i32, - width: width.max(1.) as i32, - height: height.max(1.) as i32, - }; - - popup_settings.positioner.size = None; - - // Compute popup width from the weather content. - // ~8.4px per monospace character at default font size, - // plus padding for container (12*2) and popup chrome (~32). - let max_line_len = self.full_weather.lines() - .map(|line| line.len()) - .max() - .unwrap_or(0) as f32; - // Use content-based width if available, otherwise a - // reasonable default for the wttr.in 4-column table. - let popup_width = if max_line_len > 0.0 { - (max_line_len * 8.4 + 56.0).max(400.0) - } else { - 1100.0 - }; - popup_settings.positioner.size_limits = Limits::NONE - .min_width(1.0) - .min_height(1.0) - .max_width(popup_width) - .max_height(1080.0); - - if self.full_weather.is_empty() { - let fetch_task = Task::perform(fetch_full_weather(), |result| { - cosmic::Action::App(Message::FullWeatherUpdate(result)) - }); - Task::batch([get_popup(popup_settings), fetch_task]) - } else { - get_popup(popup_settings) - } - } - } + Message::TogglePopup => self.toggle_popup(), Message::CloseRequested(id) => { if Some(id) == self.popup { self.popup = None; @@ -344,8 +293,69 @@ impl cosmic::Application for AppModel { } } +impl AppModel { + fn toggle_popup(&mut self) -> Task> { + if let Some(p) = self.popup.take() { + return destroy_popup(p); + } + + let new_id = window::Id::unique(); + self.popup = Some(new_id); + + let mut popup_settings = self.core.applet.get_popup_settings( + self.core.main_window_id().unwrap(), + new_id, + None, + None, + None, + ); + + let Rectangle { x, y, width, height } = self.rectangle; + #[allow(clippy::cast_possible_truncation)] + let anchor_rect = Rectangle:: { + x: x.max(1.) as i32, + y: y.max(1.) as i32, + width: width.max(1.) as i32, + height: height.max(1.) as i32, + }; + popup_settings.positioner.anchor_rect = anchor_rect; + + popup_settings.positioner.size = None; + + // Compute popup width from the weather content. + // ~8.4px per monospace character at default font size, + // plus padding for container (12*2) and popup chrome (~32). + #[allow(clippy::cast_precision_loss)] + let max_line_len = self.full_weather.lines() + .map(str::len) + .max() + .unwrap_or(0) as f32; + // Use content-based width if available, otherwise a + // reasonable default for the wttr.in 4-column table. + let popup_width = if max_line_len > 0.0 { + (max_line_len * 8.4 + 56.0).max(400.0) + } else { + 1100.0 + }; + popup_settings.positioner.size_limits = Limits::NONE + .min_width(1.0) + .min_height(1.0) + .max_width(popup_width) + .max_height(1080.0); + + if self.full_weather.is_empty() { + let fetch_task = Task::perform(fetch_full_weather(), |result| { + cosmic::Action::App(Message::FullWeatherUpdate(result)) + }); + Task::batch([get_popup(popup_settings), fetch_task]) + } else { + get_popup(popup_settings) + } + } +} + async fn fetch_weather() -> Result<(String, String), String> { - let response = reqwest::get(&format!("{}/?format=%l|%c|%t", WTTR_URL)) + let response = reqwest::get(&format!("{WTTR_URL}/?format=%l|%c|%t")) .await .map_err(|e| e.to_string())?; @@ -378,7 +388,7 @@ fn format_current_time() -> String { } async fn fetch_full_weather() -> Result { - let response = reqwest::get(&format!("{}?A&T", WTTR_URL)).await.map_err(|e| e.to_string())?; + let response = reqwest::get(&format!("{WTTR_URL}?A&T")).await.map_err(|e| e.to_string())?; let text = response.text().await.map_err(|e| e.to_string())?; Ok(text.trim().to_string())