Separate UI code from main.rs
- Create `widget/src/show_popup.rs`
This commit is contained in:
parent
f918795e69
commit
020be1d5e9
@ -1,10 +1,9 @@
|
|||||||
use owm_rs::free_api_v25::current::WeatherResponse;
|
use owm_rs::free_api_v25::current::WeatherResponse;
|
||||||
use owm_widg_config::config::Config;
|
use owm_widg_config::config::Config;
|
||||||
use owm_widg_config::general::ApiVersion;
|
use owm_widg_config::general::ApiVersion;
|
||||||
use slint::SharedString;
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
slint::include_modules!();
|
mod show_popup;
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let path = dirs::config_dir()
|
let path = dirs::config_dir()
|
||||||
@ -46,39 +45,19 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
"standard" => 'K',
|
"standard" => 'K',
|
||||||
_ => '?',
|
_ => '?',
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create and show the UI
|
show_popup::show_popup(
|
||||||
let ui = MainWindow::new()?;
|
city,
|
||||||
ui.set_city(SharedString::from(city));
|
country,
|
||||||
ui.set_country(SharedString::from(country));
|
weather_main,
|
||||||
ui.set_weather_main(SharedString::from(weather_main));
|
weather_description,
|
||||||
ui.set_weather_description(SharedString::from(weather_description));
|
icon,
|
||||||
ui.set_weather_icon(SharedString::from(icon_to_nerd_font(&icon)));
|
temperature,
|
||||||
ui.set_temperature(SharedString::from(temperature));
|
unit,
|
||||||
ui.set_unit(SharedString::from(unit));
|
)?;
|
||||||
|
|
||||||
ui.run()?;
|
|
||||||
}
|
}
|
||||||
other => return Err(format!("Unsupported api_version: {other:?}").into()),
|
other => return Err(format!("Unsupported api_version: {other:?}").into()),
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert OWM icon codes (e.g. "01d", "09n") to Nerd Font weather glyphs.
|
|
||||||
fn icon_to_nerd_font(code: &str) -> String {
|
|
||||||
match code {
|
|
||||||
"01d" => "", // clear day
|
|
||||||
"01n" => "", // clear night
|
|
||||||
"02d" | "02n" => "", // few clouds
|
|
||||||
"03d" | "03n" => "", // scattered clouds
|
|
||||||
"04d" | "04n" => "", // broken clouds
|
|
||||||
"09d" | "09n" => "", // shower rain
|
|
||||||
"10d" | "10n" => "", // rain
|
|
||||||
"11d" | "11n" => "", // thunderstorm
|
|
||||||
"13d" | "13n" => "", // snow
|
|
||||||
"50d" | "50n" => "", // mist
|
|
||||||
_ => "",
|
|
||||||
}
|
|
||||||
.into()
|
|
||||||
}
|
|
||||||
|
|||||||
44
widget/src/show_popup.rs
Normal file
44
widget/src/show_popup.rs
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
use slint::SharedString;
|
||||||
|
|
||||||
|
slint::include_modules!();
|
||||||
|
|
||||||
|
/// Create and show the UI window populated with weather data.
|
||||||
|
pub fn show_popup(
|
||||||
|
city: String,
|
||||||
|
country: String,
|
||||||
|
weather_main: String,
|
||||||
|
weather_description: String,
|
||||||
|
icon_code: String,
|
||||||
|
temperature: String,
|
||||||
|
unit: char,
|
||||||
|
) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let ui = MainWindow::new()?;
|
||||||
|
ui.set_city(SharedString::from(city));
|
||||||
|
ui.set_country(SharedString::from(country));
|
||||||
|
ui.set_weather_main(SharedString::from(weather_main));
|
||||||
|
ui.set_weather_description(SharedString::from(weather_description));
|
||||||
|
ui.set_weather_icon(SharedString::from(icon_to_nerd_font(&icon_code)));
|
||||||
|
ui.set_temperature(SharedString::from(temperature));
|
||||||
|
ui.set_unit(SharedString::from(unit));
|
||||||
|
|
||||||
|
ui.run()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convert OWM icon codes (e.g. "01d", "09n") to Nerd Font weather glyphs.
|
||||||
|
fn icon_to_nerd_font(code: &str) -> String {
|
||||||
|
match code {
|
||||||
|
"01d" => "", // clear day
|
||||||
|
"01n" => "", // clear night
|
||||||
|
"02d" | "02n" => "", // few clouds
|
||||||
|
"03d" | "03n" => "", // scattered clouds
|
||||||
|
"04d" | "04n" => "", // broken clouds
|
||||||
|
"09d" | "09n" => "", // shower rain
|
||||||
|
"10d" | "10n" => "", // rain
|
||||||
|
"11d" | "11n" => "", // thunderstorm
|
||||||
|
"13d" | "13n" => "", // snow
|
||||||
|
"50d" | "50n" => "", // mist
|
||||||
|
_ => "",
|
||||||
|
}
|
||||||
|
.into()
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user