add panes

This commit is contained in:
death916 2026-03-11 03:30:30 -07:00
parent a19a98c86c
commit 7d0429ebb0

View file

@ -1,8 +1,8 @@
use iced::Center;
use iced::widget::{Column, PaneGrid, button, column, text, image,row,Row,};
use iced::Element;
use iced::widget::{column, image, pane_grid, row, text};
pub fn main() -> iced::Result {
iced::application(State::default, State::update, State::view).run()
iced::run(State::update, State::view)
}
#[derive(Debug, Clone)]
@ -12,6 +12,18 @@ enum Sport {
MLB,
}
#[derive(Debug, Clone)]
enum Pane {
Main,
Sports,
}
#[derive(Debug, Clone)]
enum Message {
PaneDragged(pane_grid::DragEvent),
PaneResized(pane_grid::ResizeEvent),
}
#[derive(Debug)]
struct State {
current_time: chrono::DateTime<chrono::Utc>,
@ -20,41 +32,71 @@ struct State {
weather: Vec<u8>,
location: String,
scores: Vec<Game>,
panes: pane_grid::State<Pane>,
}
impl State {
fn update(&mut self, scores: Vec<Game>) {
self.scores = scores;
fn update(&mut self, message: Message) {
match message {
Message::PaneDragged(pane_grid::DragEvent::Dropped { pane, target }) => {
self.panes.drop(pane, target);
}
Message::PaneDragged(_) => {}
Message::PaneResized(pane_grid::ResizeEvent { split, ratio }) => {
self.panes.resize(split, ratio);
}
}
}
fn view(&self) -> Element<'_, Vec<Game>> {
let game = sports();
let games = game.scores;
let weather_img = image::Handle::from_bytes(self.weather.clone());
column![
text("scores").size(50),
//text().size(20),
text(format!("{} vs {}", games[0].team1, games[0].team2)).size(20),
text(format!("{} - {}", games[0].score1, games[0].score2)).size(20),
row![
text("Weather").size(20),
image(weather_img).width(50),
text(self.location.clone()).size(20),
]
]
.padding(20)
.align_x(Center)
fn view(state: &State) -> Element<'_, Message> {
pane_grid(&state.panes, |_pane, pane_state, _is_maximized| {
let content: Element<'_, Message> = match pane_state {
Pane::Main => {
let games = &state.scores;
let weather_img = image::Handle::from_bytes(state.weather.clone());
column![
text("scores").size(50),
text(format!("{} vs {}", games[0].team1, games[0].team2)).size(20),
text(format!("{} - {}", games[0].score1, games[0].score2)).size(20),
row![
text("Weather").size(20),
image(weather_img).width(50),
text(state.location.clone()).size(20),
]
]
.padding(20)
.align_x(Center)
.into()
}
Pane::Sports => {
let games = &state.scores;
column![
text("Scores").size(50),
text(format!("{} vs {}", games[0].team1, games[0].team2)).size(20),
text(format!("{} - {}", games[0].score1, games[0].score2)).size(20),
]
.padding(20)
.into()
}
};
pane_grid::Content::new(content)
})
.on_drag(Message::PaneDragged)
.on_resize(10, Message::PaneResized)
.into()
}
}
}
impl Default for State {
fn default() -> Self {
let text = ureq::get("https://github.com/iced-rs/iced/blob/9712b319bb7a32848001b96bd84977430f14b623/examples/resources/ferris.png?raw=true")
.call().unwrap()
.body_mut()
.read_to_vec().unwrap();
let text = ureq::get(
"https://raw.githubusercontent.com/iced-rs/iced/9712b319bb7a32848001b96bd84977430f14b623/examples/resources/ferris.png",
)
.header("User-Agent", "deathclock-app/1.0")
.call()
.unwrap()
.into_body()
.read_to_vec()
.unwrap();
State {
current_time: chrono::Utc::now(),
@ -62,7 +104,12 @@ impl Default for State {
news: Vec::new(),
weather: text,
location: "Sacramento".to_string(),
scores: Vec::new(),
scores: sports().scores,
panes: {
let (mut panes, first) = pane_grid::State::new(Pane::Main);
panes.split(pane_grid::Axis::Vertical, first, Pane::Sports);
panes
},
}
}
}
@ -102,6 +149,7 @@ fn sports() -> State {
weather: Vec::new(),
location: "Sacramento".to_string(),
scores: Vec::new(),
panes: pane_grid::State::new(Pane::Main).0,
};
state
@ -115,8 +163,7 @@ fn sports() -> State {
.push(Game::new(Sport::MLB, "Red Sox", "Yankees", "100", "95"));
println!("{:?}", state.current_time);
println!("---------------");
for game in &state.scores {
println!("+----------------------+");
println!("| Sport: {:?}", game.sport);