Skip to content

Commit

Permalink
update to iced 13.1 and new iced-multi-window dynamic dispatch para…
Browse files Browse the repository at this point in the history
…digm
  • Loading branch information
justDeeevin committed Sep 24, 2024
1 parent 1d5194c commit de2caea
Show file tree
Hide file tree
Showing 9 changed files with 1,162 additions and 1,052 deletions.
1,539 changes: 868 additions & 671 deletions Cargo.lock

Large diffs are not rendered by default.

27 changes: 11 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,29 @@ license = false
eula = false

[dependencies]
image = { version = "0.25.2", features = ["ico"] }
iced-multi-window = { git = "https://github.com/justdeeevin/iced-multi-window" }
iced_aw = { version = "0.9.3", features = [
"selection_list",
"number_input",
"quad",
"color_picker",
] }
async-std = "1.12.0"
color-eyre = "0.6.3"
async-std = "1.13.0"
color-eyre = { version = "0.6.3", default-features = false }
colorgrad = "0.7.0"
display-info = "0.5.1"
eyre = "0.6.12"
futures = "0.3.30"
geo = "0.28.0"
home = "0.5.9"
iced = { version = "0.12.1", features = [
iced = { version = "0.13.1", features = [
"image",
"canvas",
"advanced",
"async-std",
"multi-window",
] }
iced_aw = "0.11.0"
image = { version = "0.25.2", features = ["ico"] }
rdev = "0.5.3"
serde = { version = "1.0.209", features = ["derive"] }
serde_json = "1.0.127"
reqwest = { version = "0.12.7", features = ["blocking"] }
display-info = "0.5.1"
geo = "0.28.0"
serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"
zip = "2.2.0"
eyre = "0.6.12"
iced_fonts = "0.1.1"

[features]
grab = ["rdev/unstable_grab"]
Expand Down
32 changes: 16 additions & 16 deletions src/logic/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use crate::{
types::{settings::*, style::*},
};
use async_std::task::sleep;
use iced::{window, Command};
use iced::{window, Task};
use image::ImageReader;
use std::{
fs::{self, File},
time::Instant,
};

impl NuhxBoard {
pub fn load_layout(&mut self, keyboard: usize) -> Command<Message> {
pub fn load_layout(&mut self, keyboard: usize) -> Task<Message> {
self.settings.keyboard = keyboard;

self.keyboard_choice = Some(keyboard);
Expand Down Expand Up @@ -87,15 +87,15 @@ impl NuhxBoard {
self.style_choice = Some(0);

window::resize(
window::Id::MAIN,
self.main_window,
iced::Size {
width: self.layout.width,
height: self.layout.height,
},
)
}

pub fn load_style(&mut self, style: usize) -> Command<Message> {
pub fn load_style(&mut self, style: usize) -> Task<Message> {
self.settings.style = style;

self.style_choice = Some(style);
Expand Down Expand Up @@ -160,10 +160,10 @@ impl NuhxBoard {
let _ = fs::remove_file(self.keyboards_path.parent().unwrap().join("background.png"));
}

Command::none()
Task::none()
}

pub fn input_event(&mut self, event: rdev::Event) -> Command<Message> {
pub fn input_event(&mut self, event: rdev::Event) -> Task<Message> {
match event.event_type {
rdev::EventType::KeyPress(key) => {
if key == rdev::Key::CapsLock {
Expand All @@ -182,7 +182,7 @@ impl NuhxBoard {
return self.error(Error::UnknownKey(key));
};
if !self.pressed_keys.contains_key(&key_num) {
return Command::none();
return Task::none();
}
if self
.pressed_keys
Expand All @@ -192,7 +192,7 @@ impl NuhxBoard {
.as_millis()
< self.settings.min_press_time
{
return Command::perform(
return Task::perform(
sleep(std::time::Duration::from_millis(
(self.settings.min_press_time
- self
Expand All @@ -211,7 +211,7 @@ impl NuhxBoard {
}
rdev::EventType::ButtonPress(button) => {
if button == rdev::Button::Unknown(6) || button == rdev::Button::Unknown(7) {
return Command::none();
return Task::none();
}
let Ok(button) = mouse_button_code_convert(button) else {
return self.error(Error::UnknownButton(button));
Expand All @@ -224,10 +224,10 @@ impl NuhxBoard {
return self.error(Error::UnknownButton(button));
};
if button == rdev::Button::Unknown(6) || button == rdev::Button::Unknown(7) {
return Command::none();
return Task::none();
}
if !self.pressed_mouse_buttons.contains_key(&button_num) {
return Command::none();
return Task::none();
}
if self
.pressed_mouse_buttons
Expand All @@ -237,7 +237,7 @@ impl NuhxBoard {
.as_millis()
< self.settings.min_press_time
{
return Command::perform(
return Task::perform(
sleep(std::time::Duration::from_millis(
(self.settings.min_press_time
- self
Expand Down Expand Up @@ -273,7 +273,7 @@ impl NuhxBoard {

self.canvas.clear();

return Command::perform(
return Task::perform(
sleep(std::time::Duration::from_millis(
self.settings.scroll_hold_time,
)),
Expand All @@ -285,10 +285,10 @@ impl NuhxBoard {
let current_time = event.time;
let time_diff = match current_time.duration_since(self.previous_mouse_time) {
Ok(diff) => diff,
Err(_) => return Command::none(),
Err(_) => return Task::none(),
};
if time_diff.as_millis() < 10 {
return Command::none();
return Task::none();
}

let previous_pos = match self.settings.mouse_from_center {
Expand Down Expand Up @@ -317,6 +317,6 @@ impl NuhxBoard {
}
}

Command::none()
Task::none()
}
}
38 changes: 19 additions & 19 deletions src/logic/listener.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use iced::{
futures::{channel::mpsc, StreamExt},
subscription, Subscription,
use futures::{
channel::mpsc,
sink::SinkExt,
stream::{Stream, StreamExt},
};
use iced::stream;
#[cfg(feature = "grab")]
use rdev::grab;
use rdev::listen;
Expand All @@ -18,14 +20,12 @@ pub enum Event {
None,
}

pub fn bind() -> Subscription<Event> {
struct Keys;
pub fn bind() -> impl Stream<Item = Event> {
stream::channel(100, |mut output| async move {
let mut state = State::Starting;

subscription::unfold(
std::any::TypeId::of::<Keys>(),
State::Starting,
|state| async move {
match state {
loop {
match &mut state {
State::Starting => {
let (tx, rx) = mpsc::unbounded();
std::thread::spawn(move || {
Expand Down Expand Up @@ -63,16 +63,16 @@ pub fn bind() -> Subscription<Event> {
})
.unwrap();
});
(Event::Ready, State::Ready(rx))
output.send(Event::Ready).await.unwrap();
state = State::Ready(rx);
}
State::Ready(mut rx) => {
let received = rx.next().await;
match received {
Some(key) => (Event::KeyReceived(key), State::Ready(rx)),
None => (Event::None, State::Ready(rx)),
}
State::Ready(ref mut rx) => {
let Some(key) = rx.next().await else {
continue;
};
output.send(Event::KeyReceived(key)).await.unwrap();
}
}
},
)
}
})
}
41 changes: 5 additions & 36 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,12 @@ mod nuhxboard;
mod types;
mod ui;

use iced::{multi_window::Application, window};
use iced_multi_window::multi_window;
use nuhxboard::*;
use std::{
fs::{self, File},
io::{self, prelude::*},
};
use types::settings::Settings;
use ui::app::*;

multi_window! {
NuhxBoard,
Main,
SettingsWindow,
LoadKeyboard,
ErrorPopup,
KeyboardProperties,
SaveDefinitionAs,
SaveStyleAs,
KeyboardStyle,
}

static IMAGE: &[u8] = include_bytes!("../NuhxBoard.png");

fn main() -> eyre::Result<()> {
color_eyre::install()?;
Expand Down Expand Up @@ -104,25 +87,11 @@ fn main() -> eyre::Result<()> {
fs::create_dir_all(&global_path)?;
}

let settings_file = File::open(settings_path)?;

let settings: Settings = serde_json::from_reader(settings_file)?;

let icon_image = image::load_from_memory(IMAGE)?;
let icon = window::icon::from_rgba(icon_image.to_rgba8().to_vec(), 256, 256)?;

let window_settings = iced::Settings {
window: window::Settings {
size: DEFAULT_WINDOW_SIZE,
resizable: false,
icon: Some(icon),
exit_on_close_request: false,
..window::Settings::default()
},
flags: settings,
..iced::Settings::default()
};
NuhxBoard::run(window_settings)?;
iced::daemon(NuhxBoard::title, NuhxBoard::update, NuhxBoard::view)
.theme(NuhxBoard::theme)
.subscription(NuhxBoard::subscription)
.font(iced_fonts::REQUIRED_FONT_BYTES)
.run_with(NuhxBoard::new)?;

Ok(())
}
Loading

0 comments on commit de2caea

Please sign in to comment.