Begin to split out display module
Move the display to a dedicated module so that things can begin to DRY out
This commit is contained in:
@@ -0,0 +1,43 @@
|
|||||||
|
use iced::{border::Radius, widget::{button, text}, Background, Border, Color, Shadow, Theme};
|
||||||
|
use super::super::cell::Cell;
|
||||||
|
use super::Message;
|
||||||
|
|
||||||
|
const LIGHT_GRAY: Color = Color {
|
||||||
|
r: 0.5,
|
||||||
|
g: 0.5,
|
||||||
|
b: 0.5,
|
||||||
|
a: 1.0,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn widget(cell: &Cell) -> iced::Element<Message> {
|
||||||
|
let display = match cell.value() {
|
||||||
|
Some(v) => v.to_string(),
|
||||||
|
None => "?".to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
button(text(display))
|
||||||
|
.style(|theme: &Theme, status| {
|
||||||
|
let palette = theme.palette();
|
||||||
|
let mut border = Border::default().color(LIGHT_GRAY);
|
||||||
|
border.width = 1.0;
|
||||||
|
match status {
|
||||||
|
button::Status::Active => {
|
||||||
|
button::Style{
|
||||||
|
background: Some(Background::from(palette.background)),
|
||||||
|
text_color: palette.text,
|
||||||
|
border,
|
||||||
|
shadow: Shadow::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
let mut style = button::primary(theme, status);
|
||||||
|
style.border.radius = Radius::new(0);
|
||||||
|
style
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.height(50)
|
||||||
|
.width(50)
|
||||||
|
.on_press(Message::Pressed)
|
||||||
|
.into()
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum Message {
|
||||||
|
Pressed,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod cell;
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
mod cell;
|
mod cell;
|
||||||
mod board;
|
mod board;
|
||||||
|
pub mod display;
|
||||||
|
|
||||||
pub use board::Board;
|
pub use board::Board;
|
||||||
pub use board::Point;
|
pub use board::Point;
|
||||||
|
|||||||
+6
-21
@@ -1,13 +1,8 @@
|
|||||||
use iced::widget::button;
|
|
||||||
use iced::widget::text;
|
|
||||||
use rustdoku::Board;
|
use rustdoku::Board;
|
||||||
|
use rustdoku::display;
|
||||||
|
|
||||||
use iced;
|
use iced;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
enum Message {
|
|
||||||
Pressed,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct State {
|
struct State {
|
||||||
board: Board,
|
board: Board,
|
||||||
@@ -23,36 +18,26 @@ impl Default for State {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn display_cell(cell: &rustdoku::Cell) -> iced::Element<Message> {
|
fn update(state: &mut State, _message: display::Message) {
|
||||||
let display = match cell.value() {
|
|
||||||
Some(v) => v.to_string(),
|
|
||||||
None => "?".to_string(),
|
|
||||||
};
|
|
||||||
button(text(display)).on_press(Message::Pressed).into()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update(state: &mut State, _message: Message) {
|
|
||||||
state.message = "Hello, world".to_string();
|
state.message = "Hello, world".to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn display(state: &State) -> iced::Element<Message> {
|
fn display(state: &State) -> iced::Element<display::Message> {
|
||||||
let mut column: Vec<iced::Element<Message>> = Vec::with_capacity(9);
|
let mut column: Vec<iced::Element<display::Message>> = Vec::with_capacity(9);
|
||||||
for row_idx in state.board.row_range() {
|
for row_idx in state.board.row_range() {
|
||||||
let row: Vec<iced::Element<Message>> = state.board
|
let row: Vec<iced::Element<display::Message>> = state.board
|
||||||
.row_iter(row_idx)
|
.row_iter(row_idx)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|c| display_cell(c))
|
.map(|c| display::cell::widget(c))
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.collect();
|
.collect();
|
||||||
column.push(
|
column.push(
|
||||||
iced::widget::Row::with_children(row)
|
iced::widget::Row::with_children(row)
|
||||||
.spacing(3)
|
|
||||||
.into()
|
.into()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
iced::widget::Column::with_children(column)
|
iced::widget::Column::with_children(column)
|
||||||
.spacing(3)
|
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user