Rename InputOtp to OTPInput (#74)

This commit is contained in:
Floyd Wang 2024-07-26 17:12:23 +08:00 committed by GitHub
parent d278d85058
commit 55822144c4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 15 additions and 15 deletions

View file

@ -16,7 +16,7 @@ A UI components for building desktop application by using [GPUI](https://gpui.rs
- [x] Input icon - [x] Input icon
- [ ] Textarea - [ ] Textarea
- [ ] ContextMenu to let user copy, cut, paste - [ ] ContextMenu to let user copy, cut, paste
- [x] InputOTP - [x] OTPInput
- [x] Button - [x] Button
- [x] Button with Icon - [x] Button with Icon
- [x] IconButton - [x] IconButton

View file

@ -7,7 +7,7 @@ use gpui::{
use ui::{ use ui::{
button::Button, button::Button,
h_flex, h_flex,
input::{InputEvent, InputOtp, TextInput}, input::{InputEvent, OTPInput, TextInput},
prelude::FluentBuilder as _, prelude::FluentBuilder as _,
v_flex, Clickable, FocusableCycle, IconName, Size, v_flex, Clickable, FocusableCycle, IconName, Size,
}; };
@ -33,9 +33,9 @@ pub struct InputStory {
both_input1: View<TextInput>, both_input1: View<TextInput>,
large_input: View<TextInput>, large_input: View<TextInput>,
small_input: View<TextInput>, small_input: View<TextInput>,
otp_input: View<InputOtp>, otp_input: View<OTPInput>,
otp_value: Option<SharedString>, otp_value: Option<SharedString>,
opt_input2: View<InputOtp>, opt_input2: View<OTPInput>,
} }
impl InputStory { impl InputStory {
@ -87,7 +87,7 @@ impl InputStory {
.placeholder("This input have prefix and suffix.") .placeholder("This input have prefix and suffix.")
}); });
let otp_input = cx.new_view(|cx| InputOtp::new(6, cx).masked(true)); let otp_input = cx.new_view(|cx| OTPInput::new(6, cx).masked(true));
cx.subscribe(&otp_input, |this, _, ev: &InputEvent, cx| match ev { cx.subscribe(&otp_input, |this, _, ev: &InputEvent, cx| match ev {
InputEvent::Change(text) => { InputEvent::Change(text) => {
this.otp_value = Some(text.clone()); this.otp_value = Some(text.clone());
@ -123,7 +123,7 @@ impl InputStory {
both_input1, both_input1,
otp_input, otp_input,
otp_value: None, otp_value: None,
opt_input2: cx.new_view(|cx| InputOtp::new(6, cx).groups(3)), opt_input2: cx.new_view(|cx| OTPInput::new(6, cx).groups(3)),
} }
} }
@ -210,12 +210,12 @@ impl Render for InputStory {
), ),
) )
.child( .child(
section("Input OTP", cx).child( section("OTP Input", cx).child(
v_flex() v_flex()
.gap_3() .gap_3()
.child(self.otp_input.clone()) .child(self.otp_input.clone())
.when_some(self.otp_value.clone(), |this, otp| { .when_some(self.otp_value.clone(), |this, otp| {
this.child(format!("You input OTP: {}", otp)) this.child(format!("Your OTP: {}", otp))
}) })
.child(self.opt_input2.clone()), .child(self.opt_input2.clone()),
), ),

View file

@ -1,7 +1,7 @@
mod blink_cursor; mod blink_cursor;
mod history; mod history;
mod input; mod input;
mod input_otp; mod otp_input;
pub use input::*; pub use input::*;
pub use input_otp::*; pub use otp_input::*;

View file

@ -13,7 +13,7 @@ pub enum InputOptEvent {
Change(SharedString), Change(SharedString),
} }
pub struct InputOtp { pub struct OTPInput {
focus_handle: FocusHandle, focus_handle: FocusHandle,
length: usize, length: usize,
number_of_groups: usize, number_of_groups: usize,
@ -22,7 +22,7 @@ pub struct InputOtp {
blink_cursor: Model<BlinkCursor>, blink_cursor: Model<BlinkCursor>,
} }
impl InputOtp { impl OTPInput {
pub fn new(length: usize, cx: &mut ViewContext<Self>) -> Self { pub fn new(length: usize, cx: &mut ViewContext<Self>) -> Self {
let focus_handle = cx.focus_handle(); let focus_handle = cx.focus_handle();
let blink_cursor = cx.new_model(|cx| BlinkCursor::new(cx)); let blink_cursor = cx.new_model(|cx| BlinkCursor::new(cx));
@ -128,14 +128,14 @@ impl InputOtp {
} }
} }
impl FocusableView for InputOtp { impl FocusableView for OTPInput {
fn focus_handle(&self, _: &gpui::AppContext) -> FocusHandle { fn focus_handle(&self, _: &gpui::AppContext) -> FocusHandle {
self.focus_handle.clone() self.focus_handle.clone()
} }
} }
impl EventEmitter<InputEvent> for InputOtp {} impl EventEmitter<InputEvent> for OTPInput {}
impl Render for InputOtp { impl Render for OTPInput {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement { fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
let blink_show = self.blink_cursor.read(cx).visible(); let blink_show = self.blink_cursor.read(cx).visible();
let is_focused = self.focus_handle.is_focused(cx); let is_focused = self.focus_handle.is_focused(cx);