iced/examples/url_handler/src/main.rs
Héctor Ramón Jiménez 15057a05c1
Introduce center widget helper
... and also make `center_x` and `center_y` set
`width` and `height` to `Length::Fill`, respectively.

This targets the most common use case when centering
things and removes a bunch of boilerplate as a result.
2024-05-03 09:11:46 +02:00

49 lines
1.1 KiB
Rust

use iced::event::{self, Event};
use iced::widget::{center, text};
use iced::{Element, Subscription};
pub fn main() -> iced::Result {
iced::program("URL Handler - Iced", App::update, App::view)
.subscription(App::subscription)
.run()
}
#[derive(Debug, Default)]
struct App {
url: Option<String>,
}
#[derive(Debug, Clone)]
enum Message {
EventOccurred(Event),
}
impl App {
fn update(&mut self, message: Message) {
match message {
Message::EventOccurred(event) => {
if let Event::PlatformSpecific(
event::PlatformSpecific::MacOS(event::MacOS::ReceivedUrl(
url,
)),
) = event
{
self.url = Some(url);
}
}
}
}
fn subscription(&self) -> Subscription<Message> {
event::listen().map(Message::EventOccurred)
}
fn view(&self) -> Element<Message> {
let content = match &self.url {
Some(url) => text(url),
None => text("No URL received yet!"),
};
center(content.size(48)).into()
}
}