Skip to content

Instantly share code, notes, and snippets.

@floscr
Created February 9, 2024 10:48
Show Gist options
  • Save floscr/2e5664459d5218865ac1275d0a7a18c9 to your computer and use it in GitHub Desktop.
Save floscr/2e5664459d5218865ac1275d0a7a18c9 to your computer and use it in GitHub Desktop.
Iced example
use iced::theme::Theme;
use iced::widget::{column, container, text, text_input};
use iced::window::{self};
use iced::{Application, Element, Settings};
use once_cell::sync::Lazy;
#[derive(Debug, Default)]
struct State {
query: String,
}
#[derive(Debug, Clone)]
enum Message {
UpdateQuery(String),
}
static INPUT_ID: Lazy<text_input::Id> = Lazy::new(text_input::Id::unique);
impl Application for State {
type Message = Message;
type Theme = Theme;
type Executor = iced::executor::Default;
type Flags = ();
fn theme(&self) -> Theme {
Theme::Dark
}
fn new(_flags: ()) -> (State, iced::Command<Message>) {
(State::default(), text_input::focus(INPUT_ID.clone()))
}
fn title(&self) -> String {
"My iced application".to_string()
}
fn update(&mut self, message: Message) -> iced::Command<Message> {
match message {
Message::UpdateQuery(query) => {
self.query = query;
iced::Command::none()
}
}
}
fn view(&self) -> Element<Message> {
let value = text(&self.query);
let input = text_input("Query", &self.query)
.id(INPUT_ID.clone())
.on_input(Message::UpdateQuery);
let column = column![input, value].spacing(5);
container(column).padding(10).into()
}
}
pub fn main() -> iced::Result {
State::run(Settings {
window: window::Settings {
size: (700, 500),
..window::Settings::default()
},
..Settings::default()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment