Skip to content

Commit

Permalink
Adds a button for toggling the listening to keyboard input
Browse files Browse the repository at this point in the history
This commit will add a button for toggling the listening of keyboard input.
  • Loading branch information
henrikth93 committed Oct 20, 2024
1 parent 93b28b3 commit 081ba3f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
28 changes: 20 additions & 8 deletions scribe/src/bin/gui.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use iced::widget::{container, image::Handle, row, text, text_input, Column, Image};
use iced::widget::{container, image::Handle, row, text, text_input, Button, Column, Image};
use iced::{
alignment::Horizontal, executor, window, Alignment, Application, Command, Element, Font,
Length, Settings, Size, Subscription, Theme,
Expand All @@ -11,17 +11,20 @@ use std::net::TcpListener;
#[derive(Debug, Clone)]
pub enum Message {
KeyReceived(char),
ToggleListening,
NoOp,
}

struct Scribe {
keys: String,
is_listening: bool,
}

impl Default for Scribe {
fn default() -> Self {
Scribe {
keys: String::new(),
is_listening: true,
}
}
}
Expand All @@ -47,13 +50,16 @@ impl Application for Scribe {
fn update(&mut self, message: Message) -> Command<Self::Message> {
match message {
Message::KeyReceived(char) => {
let keys = &mut self.keys;
if char == '\x08' {
keys.pop();
} else {
keys.push(char);
if self.is_listening {
let keys = &mut self.keys;
if char == '\x08' {
keys.pop();
} else {
keys.push(char);
}
}
}
Message::ToggleListening => self.is_listening = !self.is_listening,
Message::NoOp => todo!(),
}
Command::none()
Expand Down Expand Up @@ -84,7 +90,12 @@ impl Application for Scribe {

fn view(&self) -> Element<Message> {
let logo_data: &[u8] = include_bytes!("../../ScribeBtnPadBlack.png");
let logo: Image<Handle> = Image::new(Handle::from_memory(logo_data.to_vec())).width(50);
let logo_handle = Handle::from_memory(logo_data.to_vec());
let logo_button: Image<Handle> = Image::new(logo_handle.clone()).width(50);
let logo_row: Image<Handle> = Image::new(logo_handle).width(50);

let listening_button: Button<'_, Message, Theme> =
Button::new(logo_button).on_press(Message::ToggleListening);

let text_for_translation = text_input("Your translation here ...", &self.keys.clone())
.font(Font::DEFAULT)
Expand All @@ -93,7 +104,8 @@ impl Application for Scribe {
let title_row = container(row!(text("Welcome to Scribe").size(30)))
.width(Length::Fill)
.align_x(Horizontal::Center);
let content_row = row!(logo, text_for_translation).align_items(Alignment::Center);
let content_row =
row!(listening_button, text_for_translation).align_items(Alignment::Center);

Column::new()
.width(Length::Fill)
Expand Down
3 changes: 2 additions & 1 deletion scribe/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use iced::{widget::text_input, Background, Border, Color};
use iced::{widget::button, widget::text_input, Background, Border, Color};
use rdev::Key;

pub fn allowed_keys(key: &Key) -> Option<char> {
Expand Down Expand Up @@ -57,6 +57,7 @@ pub fn allowed_keys(key: &Key) -> Option<char> {
}

pub struct CustomTextInput;
pub struct CustomButtonStyle;

impl text_input::StyleSheet for CustomTextInput {
type Style = iced::Theme;
Expand Down

0 comments on commit 081ba3f

Please sign in to comment.