Temp commit for save, nothing works yet :)
This commit is contained in:
parent
3e8e4def73
commit
a8ad1d5c76
2544
Cargo.lock
generated
2544
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -9,4 +9,4 @@ edition = "2021"
|
||||
libusb = "0.3.0"
|
||||
colored = "2.0.0"
|
||||
json = "0.12.4"
|
||||
gtk = { version = "0.4.8", package = "gtk4" }
|
||||
iced = "0.7.0"
|
||||
207
src/main.rs
207
src/main.rs
@ -7,9 +7,9 @@ use std::{cell::Cell, rc::Rc};
|
||||
|
||||
use colored::Colorize;
|
||||
|
||||
use gtk::glib::clone;
|
||||
use gtk::{prelude::*};
|
||||
use gtk::{Application, ApplicationWindow, Button};
|
||||
use iced::{Sandbox, Settings, Vector};
|
||||
use iced::widget::{Row, Column, Button, Text, Container, Slider};
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
struct KeyboardLighting {
|
||||
@ -18,7 +18,7 @@ struct KeyboardLighting {
|
||||
cols: u16
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone)]
|
||||
struct KeyboardLayout {
|
||||
rows: u16,
|
||||
cols: u16,
|
||||
@ -45,6 +45,24 @@ struct LightingMessage {
|
||||
}
|
||||
*/
|
||||
|
||||
struct MainPage {
|
||||
red: u8,
|
||||
green: u8,
|
||||
blue: u8,
|
||||
config: KeyboardConfig,
|
||||
btn_appearance: iced::widget::button::Appearance,
|
||||
btn_size: u16
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum MainPageMessages {
|
||||
UpdateColor {
|
||||
row: u8,
|
||||
col: u8
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn parse_config(config_path: &Path) -> KeyboardConfig{
|
||||
let mut parsed_json = json::parse(&std::fs::read_to_string(config_path).unwrap()).unwrap();
|
||||
|
||||
@ -97,15 +115,13 @@ fn parse_config(config_path: &Path) -> KeyboardConfig{
|
||||
return keyboard_config;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
fn main() -> Result<(), iced::Error> {
|
||||
// Printing the parsed config for debug purposes
|
||||
let config: KeyboardConfig = parse_config(&Path::new("numpad_config.json"));
|
||||
println!("{:#?}", config);
|
||||
|
||||
// Creating the application and displaying it
|
||||
let app = Application::builder().application_id("org.sebaweb.keyboard-control-software").build();
|
||||
app.connect_activate(build_ui);
|
||||
app.run();
|
||||
MainPage::run(Settings::default())
|
||||
}
|
||||
|
||||
// USB Code
|
||||
@ -146,74 +162,117 @@ fn send_lighting_update(col: u8, row: u8, r: u8, g: u8, b: u8, _a: u8) {
|
||||
}
|
||||
|
||||
// UI Code
|
||||
fn build_ui(app: &Application) {
|
||||
impl Sandbox for MainPage {
|
||||
type Message = MainPageMessages;
|
||||
|
||||
let config: KeyboardConfig = parse_config(&Path::new("numpad_config.json"));
|
||||
|
||||
|
||||
let flow_box = gtk::FlowBox::builder()
|
||||
.max_children_per_line(config.layout.cols.into())
|
||||
.build();
|
||||
|
||||
for i in 0..config.layout.keys.len() {
|
||||
let button = Button::builder()
|
||||
.label(&config.layout.keys[i])
|
||||
.focusable(true)
|
||||
.build();
|
||||
|
||||
let cols = Rc::new(Cell::new(config.lighting.cols));
|
||||
let rows = Rc::new(Cell::new(config.lighting.rows));
|
||||
button.connect_clicked(clone!(@strong cols, @strong rows => move |button| {
|
||||
let color = gtk::ColorChooserDialog::builder()
|
||||
.default_width(400)
|
||||
.default_height(200)
|
||||
.name("Color picker")
|
||||
.visible(true)
|
||||
.use_alpha(true)
|
||||
.use_header_bar(1)
|
||||
.hide_on_close(true)
|
||||
.build();
|
||||
|
||||
color.display();
|
||||
|
||||
color.connect_close(move |color| {
|
||||
println!("The user requested to close the window with a keybind");
|
||||
println!("{}", color.rgba());
|
||||
});
|
||||
|
||||
let num = Rc::new(Cell::new(i));
|
||||
color.connect_response(clone!(@strong num, @strong cols, @strong rows => move |color, response| {
|
||||
println!("The user responded to the window with as cresponse type {}", response);
|
||||
|
||||
if response == gtk::ResponseType::Ok {
|
||||
let red = color.rgba().red()*255f32;
|
||||
let green = color.rgba().green()*255f32;
|
||||
let blue = color.rgba().blue()*255f32;
|
||||
let alpha = color.rgba().alpha()*255f32;
|
||||
|
||||
let keys_per_row = cols.get();
|
||||
let row = (num.get() as f32 / keys_per_row as f32).floor();
|
||||
let col = num.get() as f32 - row*keys_per_row as f32;
|
||||
|
||||
println!("i: {}, row: {}, col: {}, red: {}, green: {}, blue: {}, alpha: {}", i, row, col, red as u8, green as u8, blue as u8, alpha as u8);
|
||||
send_lighting_update(col as u8, row as u8, red as u8, green as u8, blue as u8, alpha as u8);
|
||||
fn new() -> Self {
|
||||
println!("Creating UI");
|
||||
MainPage {
|
||||
red: 0, green: 0, blue: 0,
|
||||
config: parse_config(&Path::new("numpad_config.json")),
|
||||
btn_appearance: iced::widget::button::Appearance {
|
||||
shadow_offset: Vector {
|
||||
x: 0.0,
|
||||
y: 0.0
|
||||
},
|
||||
background: iced::Color::from_rgb8(0xA, 0xA, 0xA).into(),
|
||||
border_radius: 10.0,
|
||||
border_width: 10.0,
|
||||
border_color: iced::Color::from_rgb8(0xFF, 0xFF, 0xFF),
|
||||
text_color: iced::Color::from_rgb8(0xFF, 0xFF, 0xFF)
|
||||
},
|
||||
btn_size: 150
|
||||
}
|
||||
color.destroy();
|
||||
}));
|
||||
|
||||
println!("The label of the button is {}", button.label().unwrap());
|
||||
}));
|
||||
|
||||
flow_box.insert(&button, i.try_into().unwrap());
|
||||
}
|
||||
|
||||
let window = ApplicationWindow::builder()
|
||||
.application(app)
|
||||
.title("Keyboard Control Software")
|
||||
.default_width(1024)
|
||||
.default_height(900)
|
||||
.child(&flow_box)
|
||||
.build();
|
||||
|
||||
window.present();
|
||||
fn title(&self) -> String {
|
||||
String::from("Sebaweb Keyboard Control Software")
|
||||
}
|
||||
|
||||
fn update(&mut self, message: Self::Message) {
|
||||
println!("New message: {:?}", message);
|
||||
}
|
||||
|
||||
fn view(&self) -> iced::Element<'_, Self::Message> {
|
||||
// TODO: Make dynamic layout
|
||||
|
||||
|
||||
// Create the layout
|
||||
let btn_num_lck = Button::new("Num\nLock").height(iced::Length::Units(self.btn_size)).width(iced::Length::Units(self.btn_size)).on_press(MainPageMessages::UpdateColor { row: 0, col: 0 });
|
||||
let btn_divide = Button::new("/").height(iced::Length::Units(self.btn_size)).width(iced::Length::Units(self.btn_size)).on_press(MainPageMessages::UpdateColor { row: 0, col: 1 });
|
||||
let btn_multiply = Button::new("*").height(iced::Length::Units(self.btn_size)).width(iced::Length::Units(self.btn_size)).on_press(MainPageMessages::UpdateColor { row: 0, col: 2 });
|
||||
let btn_subtract = Button::new("-").height(iced::Length::Units(self.btn_size)).width(iced::Length::Units(self.btn_size)).on_press(MainPageMessages::UpdateColor { row: 0, col: 3 });
|
||||
|
||||
let row_1 = Row::new()
|
||||
.push(btn_num_lck)
|
||||
.push(btn_divide)
|
||||
.push(btn_multiply)
|
||||
.push(btn_subtract)
|
||||
;
|
||||
|
||||
let btn_7 = Button::new("7").height(iced::Length::Units(self.btn_size)).width(iced::Length::Units(self.btn_size)).on_press(MainPageMessages::UpdateColor { row: 1, col: 0 });
|
||||
let btn_8 = Button::new("8").height(iced::Length::Units(self.btn_size)).width(iced::Length::Units(self.btn_size)).on_press(MainPageMessages::UpdateColor { row: 1, col: 1 });
|
||||
let btn_9= Button::new("9").height(iced::Length::Units(self.btn_size)).width(iced::Length::Units(self.btn_size)).on_press(MainPageMessages::UpdateColor { row: 1, col: 2 });
|
||||
let btn_plus = Button::new("+").height(iced::Length::Units(self.btn_size*2)).width(iced::Length::Units(self.btn_size)).on_press(MainPageMessages::UpdateColor { row: 1, col: 3 });
|
||||
|
||||
let row_2 = Row::new()
|
||||
.push(btn_7)
|
||||
.push(btn_8)
|
||||
.push(btn_9)
|
||||
;
|
||||
|
||||
let btn_4 = Button::new("4").height(iced::Length::Units(self.btn_size)).width(iced::Length::Units(self.btn_size)).on_press(MainPageMessages::UpdateColor { row: 2, col: 0 });
|
||||
let btn_5 = Button::new("5").height(iced::Length::Units(self.btn_size)).width(iced::Length::Units(self.btn_size)).on_press(MainPageMessages::UpdateColor { row: 2, col: 1 });
|
||||
let btn_6= Button::new("6").height(iced::Length::Units(self.btn_size)).width(iced::Length::Units(self.btn_size)).on_press(MainPageMessages::UpdateColor { row: 2, col: 2 });
|
||||
|
||||
let row_3 = Row::new()
|
||||
.push(btn_4)
|
||||
.push(btn_5)
|
||||
.push(btn_6)
|
||||
;
|
||||
|
||||
let middle_buttons = Column::new().push(row_2).push(row_3);
|
||||
let middle_buttons_with_plus = Row::new().push(middle_buttons).push(btn_plus);
|
||||
|
||||
let btn_1 = Button::new("1").height(iced::Length::Units(self.btn_size)).width(iced::Length::Units(self.btn_size)).on_press(MainPageMessages::UpdateColor { row: 3, col: 0 });
|
||||
let btn_2 = Button::new("2").height(iced::Length::Units(self.btn_size)).width(iced::Length::Units(self.btn_size)).on_press(MainPageMessages::UpdateColor { row: 3, col: 1 });
|
||||
let btn_3= Button::new("3").height(iced::Length::Units(self.btn_size)).width(iced::Length::Units(self.btn_size)).on_press(MainPageMessages::UpdateColor { row: 3, col: 2 });
|
||||
let btn_enter = Button::new("Enter").height(iced::Length::Units(self.btn_size*2)).width(iced::Length::Units(self.btn_size)).on_press(MainPageMessages::UpdateColor { row: 3, col: 3 });
|
||||
|
||||
let row_4 = Row::new()
|
||||
.push(btn_1)
|
||||
.push(btn_2)
|
||||
.push(btn_3)
|
||||
;
|
||||
|
||||
let btn_0 = Button::new("0").height(iced::Length::Units(self.btn_size)).width(iced::Length::Units(self.btn_size*2)).on_press(MainPageMessages::UpdateColor { row: 4, col: 0 });
|
||||
let btn_comma = Button::new(",").height(iced::Length::Units(self.btn_size)).width(iced::Length::Units(self.btn_size)).on_press(MainPageMessages::UpdateColor { row: 4, col: 1 });
|
||||
|
||||
let row_5 = Row::new()
|
||||
.push(btn_0)
|
||||
.push(btn_comma)
|
||||
;
|
||||
|
||||
let bottom_buttons = Column::new().push(row_4).push(row_5);
|
||||
let bottom_buttons_with_comma = Row::new().push(bottom_buttons).push(btn_enter);
|
||||
|
||||
let col = Column::new()
|
||||
.push(row_1)
|
||||
.push(middle_buttons_with_plus)
|
||||
.push(bottom_buttons_with_comma)
|
||||
.height(iced::Length::Fill).width(iced::Length::Fill);
|
||||
|
||||
// Create the color selector
|
||||
|
||||
let slider_r = Slider::new();
|
||||
let color_selector_col = Column::new()
|
||||
.push(slider_r)
|
||||
.height(iced::Length::Fill).width(iced::Length::Fill);
|
||||
|
||||
let view_row = Row::new().height(iced::Length::Fill).width(iced::Length::Fill)
|
||||
.push(col)
|
||||
.push(color_selector_col);
|
||||
|
||||
view_row.into()
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user