Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a button for toggling the listening to keyboard input #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading