From e5649a0befdd4d3cb0ec0a4fc6dd54554e66f616 Mon Sep 17 00:00:00 2001 From: Peter Sonntaga Date: Sat, 15 Jan 2022 17:46:58 +0100 Subject: [PATCH 1/6] add missing verses --- .../libadwaita/examples/advent_calendar.rs | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/relm4-examples/libadwaita/examples/advent_calendar.rs b/relm4-examples/libadwaita/examples/advent_calendar.rs index c386662e..bb2cb332 100644 --- a/relm4-examples/libadwaita/examples/advent_calendar.rs +++ b/relm4-examples/libadwaita/examples/advent_calendar.rs @@ -232,18 +232,19 @@ fn main() { ("Then spake Jesus again unto them, saying, I am the light of the world: he that followeth me shall not walk in darkness, but shall have the light of life.", "John 8, 12"), ("Rejoice in the Lord alway: and again I say, Rejoice. Let your moderation be known unto all men. The Lord is at hand.", "Philippians 4, 4-5"), ("For the word of God is quick, and powerful, and sharper than any twoedged sword, piercing even to the dividing asunder of soul and spirit, and of the joints and marrow, and is a discerner of the thoughts and intents of the heart.", "Hebrews 4, 12"), - ("V13", "P13"), - ("V14", "P14"), - ("V15", "P15"), - ("V16", "P16"), - ("V17", "P17"), - ("V18", "P18"), - ("V19", "P19"), - ("V20", "P20"), - ("V21", "P21"), - ("V22", "P22"), - ("V23", "P23"), - ("V24", "P24"), + ("Commit to the Lord whatever you do, and he will establish your plans.", "Proverbs 16:3"), + ("Let the morning bring me word of your unfailing love, for I have put my trust in you. Show me the way I should go, for to you I entrust my life.", "Psalm 143:8"), + ("Be completely humble and gentle; be patient, bearing with one another in love.", "Ephesians 4:2"), + ("Whatever you do, work at it with all your heart, as working for the Lord, not for human masters, since you know that you will receive an inheritance from the Lord as a reward. It is the Lord Christ you are serving.", "Colossians 3:23-24"), + ("Let love and faithfulness never leave you;bind them around your neck, write them on the tablet of your heart. Then you will win favor and a good name in the sight of God and man.", "Proverbs 3:3-4"), + ("So do not fear, for I am with you; do not be dismayed, for I am your God. I will strengthen you and help you; I will uphold you with my righteous right hand.", "Isaiah 41:10"), + ("But blessed is the one who trusts in the Lord, whose confidence is in him. They will be like a tree planted by the water that sends out its roots by the stream. It does not fear when heat comes; its leaves are always green. It has no worries in a year of drought and never fails to bear fruit.", "Jeremiah 17:7-8"), + ("Love is patient, love is kind. It does not envy, it does not boast, it is not proud. It does not dishonor others, it is not self-seeking, it is not easily angered, it keeps no record of wrongs.", "1 Corinthians 13:4-5"), + ("Do not be anxious about anything, but in every situation, by prayer and petition, with thanksgiving, present your requests to God. And the peace of God, which transcends all understanding, will guard your hearts and your minds in Christ Jesus.", "Philippians 4:6-7"), + ("These commandments that I give you today are to be on your hearts. Impress them on your children. Talk about them when you sit at home and when you walk along the road, when you lie down and when you get up.", "Deuteronomy 6:6-7"), + ("But blessed is the one who trusts in the Lord, whose confidence is in him. They will be like a tree planted by the water that sends out its roots by the stream. It does not fear when heat comes; its leaves are always green. It has no worries in a year of drought and never fails to bear fruit.", "Jeremiah 17:7-8"), + ("The Lord bless you and keep you; the Lord make his face shine on you and be gracious to you; + the Lord turn his face toward you and give you peace.", "Numbers 6:24-26"), ]; // Fill factory with the verses From 94fa5b74e760e1ee4e173b30301a95bd70748a75 Mon Sep 17 00:00:00 2001 From: Peter Sonntaga Date: Sat, 15 Jan 2022 17:46:58 +0100 Subject: [PATCH 2/6] add missing verses --- .../libadwaita/examples/advent_calendar.rs | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/relm4-examples/libadwaita/examples/advent_calendar.rs b/relm4-examples/libadwaita/examples/advent_calendar.rs index c386662e..6307cce4 100644 --- a/relm4-examples/libadwaita/examples/advent_calendar.rs +++ b/relm4-examples/libadwaita/examples/advent_calendar.rs @@ -232,18 +232,18 @@ fn main() { ("Then spake Jesus again unto them, saying, I am the light of the world: he that followeth me shall not walk in darkness, but shall have the light of life.", "John 8, 12"), ("Rejoice in the Lord alway: and again I say, Rejoice. Let your moderation be known unto all men. The Lord is at hand.", "Philippians 4, 4-5"), ("For the word of God is quick, and powerful, and sharper than any twoedged sword, piercing even to the dividing asunder of soul and spirit, and of the joints and marrow, and is a discerner of the thoughts and intents of the heart.", "Hebrews 4, 12"), - ("V13", "P13"), - ("V14", "P14"), - ("V15", "P15"), - ("V16", "P16"), - ("V17", "P17"), - ("V18", "P18"), - ("V19", "P19"), - ("V20", "P20"), - ("V21", "P21"), - ("V22", "P22"), - ("V23", "P23"), - ("V24", "P24"), + ("Commit to the Lord whatever you do, and he will establish your plans.", "Proverbs 16:3"), + ("Let the morning bring me word of your unfailing love, for I have put my trust in you. Show me the way I should go, for to you I entrust my life.", "Psalm 143:8"), + ("Be completely humble and gentle; be patient, bearing with one another in love.", "Ephesians 4:2"), + ("Whatever you do, work at it with all your heart, as working for the Lord, not for human masters, since you know that you will receive an inheritance from the Lord as a reward. It is the Lord Christ you are serving.", "Colossians 3:23-24"), + ("Let love and faithfulness never leave you;bind them around your neck, write them on the tablet of your heart. Then you will win favor and a good name in the sight of God and man.", "Proverbs 3:3-4"), + ("So do not fear, for I am with you; do not be dismayed, for I am your God. I will strengthen you and help you; I will uphold you with my righteous right hand.", "Isaiah 41:10"), + ("But blessed is the one who trusts in the Lord, whose confidence is in him. They will be like a tree planted by the water that sends out its roots by the stream. It does not fear when heat comes; its leaves are always green. It has no worries in a year of drought and never fails to bear fruit.", "Jeremiah 17:7-8"), + ("Love is patient, love is kind. It does not envy, it does not boast, it is not proud. It does not dishonor others, it is not self-seeking, it is not easily angered, it keeps no record of wrongs.", "1 Corinthians 13:4-5"), + ("Do not be anxious about anything, but in every situation, by prayer and petition, with thanksgiving, present your requests to God. And the peace of God, which transcends all understanding, will guard your hearts and your minds in Christ Jesus.", "Philippians 4:6-7"), + ("These commandments that I give you today are to be on your hearts. Impress them on your children. Talk about them when you sit at home and when you walk along the road, when you lie down and when you get up.", "Deuteronomy 6:6-7"), + ("But blessed is the one who trusts in the Lord, whose confidence is in him. They will be like a tree planted by the water that sends out its roots by the stream. It does not fear when heat comes; its leaves are always green. It has no worries in a year of drought and never fails to bear fruit.", "Jeremiah 17:7-8"), + ("The Lord bless you and keep you; the Lord make his face shine on you and be gracious to you; the Lord turn his face toward you and give you peace.", "Numbers 6:24-26"), ]; // Fill factory with the verses From fec21da482d42e542024dfe32253418506b21f1e Mon Sep 17 00:00:00 2001 From: Peter Sonntaga Date: Sun, 23 Jan 2022 21:59:19 +0100 Subject: [PATCH 3/6] add init version of sudoku solver example --- relm4-examples/libadwaita/Cargo.toml | 2 + .../libadwaita/examples/sudoku_solver.rs | 197 ++++++++++++++++++ 2 files changed, 199 insertions(+) create mode 100644 relm4-examples/libadwaita/examples/sudoku_solver.rs diff --git a/relm4-examples/libadwaita/Cargo.toml b/relm4-examples/libadwaita/Cargo.toml index b9d426cd..23a047e7 100644 --- a/relm4-examples/libadwaita/Cargo.toml +++ b/relm4-examples/libadwaita/Cargo.toml @@ -8,3 +8,5 @@ edition = "2021" rand = "0.8.4" relm4 = { path = "../../", features = ["libadwaita", "macros"] } relm4-components = { path = "../../relm4-components" } +# For Sudoku example +sudoku = "0.7.0" \ No newline at end of file diff --git a/relm4-examples/libadwaita/examples/sudoku_solver.rs b/relm4-examples/libadwaita/examples/sudoku_solver.rs new file mode 100644 index 00000000..06675f51 --- /dev/null +++ b/relm4-examples/libadwaita/examples/sudoku_solver.rs @@ -0,0 +1,197 @@ +use gtk::glib::Sender; +use gtk::prelude::{BoxExt, ButtonExt, GtkWindowExt, OrientableExt}; +use relm4::adw::prelude::WidgetExt; +use relm4::adw::traits::AdwApplicationWindowExt; +use relm4::factory::positions::GridPosition; +use relm4::factory::{FactoryPrototype, FactoryVec}; +use relm4::{adw, gtk, send, AppUpdate, Model, RelmApp, WidgetPlus, Widgets}; +use sudoku::Sudoku; + +#[derive(Debug)] +enum AppMsg { + Solve, + Clear, + Update(usize, u8), +} + +#[derive(Clone, Debug)] +struct Number { + value: u8, +} + +struct AppModel { + values: FactoryVec, +} + +impl AppModel { + fn empty() -> Self { + Self { + values: FactoryVec::from_vec(vec![Number { value: 0 }; 81]), + } + } + + fn to_line_string(&self) -> String { + let mut res = String::new(); + for v in self.values.iter() { + let string = match v.value { + 0 => String::from("."), + v @ 1..=9 => format!("{v}"), + v => panic!("{v} is not defined"), + }; + res.push_str(string.as_str()) + } + res + } + + fn from_string(&mut self, input: String) { + for (i, v) in input.chars().enumerate() { + if v != '.' { + let value = v.to_string().parse().unwrap(); + if let Some(v) = self.values.get_mut(i) { + v.value = value; + } + } + } + } +} + +impl Model for AppModel { + type Msg = AppMsg; + type Widgets = AppWidgets; + type Components = (); +} + +impl AppUpdate for AppModel { + fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender) -> bool { + match msg { + AppMsg::Solve => { + let content = self.to_line_string(); + println!("Start solving with {content}"); + let sudoku = Sudoku::from_str_line(content.as_str()).unwrap(); + + if let Some(solution) = sudoku.solve_one() { + self.from_string(solution.to_string()); + } + } + AppMsg::Clear => { + // Why does this not work + // self.values.clear(); + // self.values = AppModel::empty().values; + + // workaround + for index in 0..self.values.len() { + if let Some(v) = self.values.get_mut(index) { + v.value = 0; + } + } + } + AppMsg::Update(index, value) => { + if let Some(v) = self.values.get_mut(index) { + v.value = value; + } + } + } + true + } +} + +#[derive(Debug)] +struct FactoryWidgets { + value: gtk::DropDown, +} + +impl FactoryPrototype for Number { + type Factory = FactoryVec; + type Widgets = FactoryWidgets; + type Root = gtk::DropDown; + type View = gtk::Grid; + type Msg = AppMsg; + + fn init_view(&self, index: &usize, sender: Sender) -> FactoryWidgets { + let dropdown = + gtk::DropDown::from_strings(&[" ", "1", "2", "3", "4", "5", "6", "7", "8", "9"]); + + if index % 3 == 0 { + dropdown.set_margin_start(5); + } + if (index / 9) % 3 == 0 { + dropdown.set_margin_top(5); + } + // When available: + // dropdown.set_sh(false); + let index = *index; + dropdown.connect_selected_notify(move |d| { + sender + .send(AppMsg::Update(index, d.selected() as u8)) + .unwrap(); + }); + + FactoryWidgets { value: dropdown } + } + + fn position(&self, index: &usize) -> GridPosition { + let index = *index as i32; + + let row = index / 9; + let column = index % 9; + + GridPosition { + column, + row, + width: 1, + height: 1, + } + } + + fn view(&self, _index: &usize, widgets: &FactoryWidgets) { + widgets.value.set_selected(self.value as u32); + } + + fn root_widget(widgets: &FactoryWidgets) -> >k::DropDown { + &widgets.value + } +} + +#[relm4::widget] +impl Widgets for AppWidgets { + view! { + adw::ApplicationWindow { + set_default_width: 100, + set_default_height: 100, + set_resizable: false, + set_content = Some(>k::Box) { + set_orientation: gtk::Orientation::Vertical, + set_margin_all: 5, + set_spacing: 5, + append = &adw::HeaderBar { + set_title_widget = Some(&adw::WindowTitle::new("Sudoku Solver", + "Beat your neighbor in solving Sudoku puzzles")) { + }, + pack_start =>k::Button { + set_label: "Clear", + connect_clicked(sender) => move |_| { + send!(sender, AppMsg::Clear); + } + }, + pack_end = >k::Button { + set_label: "Solve", + connect_clicked(sender) => move |_| { + send!(sender, AppMsg::Solve); + } + }, + }, + append = >k::Grid { + set_margin_all: 5, + factory!(model.values), + } + } + } + } +} + +fn main() { + let model = AppModel::empty(); + + let relm = RelmApp::new(model); + relm.run(); +} From 172c1e78afdc24d22c939deaa1cb2ea3bb33f8d4 Mon Sep 17 00:00:00 2001 From: Peter Sonntaga Date: Mon, 24 Jan 2022 19:20:40 +0100 Subject: [PATCH 4/6] remove debugging info --- relm4-examples/libadwaita/examples/sudoku_solver.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/relm4-examples/libadwaita/examples/sudoku_solver.rs b/relm4-examples/libadwaita/examples/sudoku_solver.rs index 06675f51..b99072d6 100644 --- a/relm4-examples/libadwaita/examples/sudoku_solver.rs +++ b/relm4-examples/libadwaita/examples/sudoku_solver.rs @@ -24,7 +24,7 @@ struct AppModel { } impl AppModel { - fn empty() -> Self { + fn new() -> Self { Self { values: FactoryVec::from_vec(vec![Number { value: 0 }; 81]), } @@ -66,7 +66,6 @@ impl AppUpdate for AppModel { match msg { AppMsg::Solve => { let content = self.to_line_string(); - println!("Start solving with {content}"); let sudoku = Sudoku::from_str_line(content.as_str()).unwrap(); if let Some(solution) = sudoku.solve_one() { @@ -76,7 +75,7 @@ impl AppUpdate for AppModel { AppMsg::Clear => { // Why does this not work // self.values.clear(); - // self.values = AppModel::empty().values; + // self.values = AppModel::new().values; // workaround for index in 0..self.values.len() { @@ -190,7 +189,7 @@ impl Widgets for AppWidgets { } fn main() { - let model = AppModel::empty(); + let model = AppModel::new(); let relm = RelmApp::new(model); relm.run(); From 1ad01b035a2c48c8d8ea04da64f67f798b9ca59a Mon Sep 17 00:00:00 2001 From: Peter Sonntaga Date: Thu, 27 Jan 2022 21:13:34 +0100 Subject: [PATCH 5/6] remove comments --- relm4-examples/libadwaita/examples/sudoku_solver.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/relm4-examples/libadwaita/examples/sudoku_solver.rs b/relm4-examples/libadwaita/examples/sudoku_solver.rs index b99072d6..adbf72be 100644 --- a/relm4-examples/libadwaita/examples/sudoku_solver.rs +++ b/relm4-examples/libadwaita/examples/sudoku_solver.rs @@ -73,11 +73,6 @@ impl AppUpdate for AppModel { } } AppMsg::Clear => { - // Why does this not work - // self.values.clear(); - // self.values = AppModel::new().values; - - // workaround for index in 0..self.values.len() { if let Some(v) = self.values.get_mut(index) { v.value = 0; From f21f553e33683ffefbc8a680d540e602b4abb794 Mon Sep 17 00:00:00 2001 From: Peter Sonntaga Date: Thu, 27 Jan 2022 21:25:21 +0100 Subject: [PATCH 6/6] correct comment --- relm4-examples/libadwaita/examples/sudoku_solver.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/relm4-examples/libadwaita/examples/sudoku_solver.rs b/relm4-examples/libadwaita/examples/sudoku_solver.rs index adbf72be..02107165 100644 --- a/relm4-examples/libadwaita/examples/sudoku_solver.rs +++ b/relm4-examples/libadwaita/examples/sudoku_solver.rs @@ -111,8 +111,9 @@ impl FactoryPrototype for Number { if (index / 9) % 3 == 0 { dropdown.set_margin_top(5); } + // When available: - // dropdown.set_sh(false); + // dropdown.set_show_arrow(false); let index = *index; dropdown.connect_selected_notify(move |d| { sender