From cd613db6d68069053c6e17150618f1bb38ea7fb3 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Fri, 24 Jan 2025 23:39:19 +0800 Subject: [PATCH] number_input: Improve NumberInput to better handle mouse down position cursor and fix sizable API. (#582) Close #579 - Leave some space before the Input to support mouse down to position cursor at start of line. - Fix to match with size appearance. - Keep padding left and right to Input, even setup `appearance: false`. ## Break Changes - The `xlarge`, `large`, `small`, `xsmall` ... method has been moved to use `ui::Sizable` trait to same as other component. --- Cargo.lock | 152 ++++++++++++++-------------- crates/story/src/input_story.rs | 2 +- crates/ui/src/input/input.rs | 4 +- crates/ui/src/input/number_input.rs | 78 ++++++++------ 4 files changed, 125 insertions(+), 111 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e43db36e..e44d683b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -96,7 +96,7 @@ checksum = "0ae92a5119aa49cdbcf6b9f893fe4e1d98b04ccbf82ee0584ad948a44a734dea" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -351,7 +351,7 @@ checksum = "3b43422f69d8ff38f95f1b2bb76517c91589a924d1559a0e935d7c8ce0274c11" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -386,7 +386,7 @@ checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -474,7 +474,7 @@ dependencies = [ "bitflags 2.6.0", "cexpr", "clang-sys", - "itertools 0.13.0", + "itertools 0.11.0", "log", "prettyplease", "proc-macro2", @@ -482,7 +482,7 @@ dependencies = [ "regex", "rustc-hash 1.1.0", "shlex", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -563,7 +563,7 @@ source = "git+https://github.com/kvark/blade?rev=b16f5c7bd873c7126f48c82c39e7ae6 dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -662,7 +662,7 @@ checksum = "bcfcc3cd946cb52f0bbfdbbcfa2f4e24f75ebb6c0e1002f7c25904fada18b9ec" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -750,13 +750,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fce8dd7fcfcbf3a0a87d8f515194b49d6135acab73e18bd380d1d93bb1a15eb" dependencies = [ "heck 0.4.1", - "indexmap 2.7.0", + "indexmap 2.7.1", "log", "proc-macro2", "quote", "serde", "serde_json", - "syn 2.0.95", + "syn 2.0.96", "tempfile", "toml 0.8.19", ] @@ -840,7 +840,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "218a07f694e17fb902c2fe72ba31ab821f2d57c146d51761aae00ab5614a3727" dependencies = [ "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -954,7 +954,7 @@ name = "collections" version = "0.1.0" source = "git+https://github.com/huacnlee/zed.git?branch=export-platform-window#1c322c0f2daa50f46644832a6a1cdf15bcf921ea" dependencies = [ - "indexmap 2.7.0", + "indexmap 2.7.1", "rustc-hash 2.1.0", ] @@ -1238,7 +1238,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13b588ba4ac1a99f7f2964d24b3d896ddc6bf847ee3855dbd4366f058cfcd331" dependencies = [ "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -1248,7 +1248,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "edb49164822f3ee45b17acd4a208cfc1251410cf0cad9a833234c9890774dd9f" dependencies = [ "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -1271,7 +1271,7 @@ dependencies = [ "ident_case", "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -1282,7 +1282,7 @@ checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" dependencies = [ "darling_core", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -1310,7 +1310,7 @@ dependencies = [ "proc-macro2", "quote", "rustc_version", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -1426,7 +1426,7 @@ dependencies = [ "darling", "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -1506,7 +1506,7 @@ checksum = "de0d48a183585823424a4ce1aa132d174a6a81bd540895822eb4c8373a8e49e8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -1806,7 +1806,7 @@ checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -1929,7 +1929,7 @@ checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -2176,7 +2176,7 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -2423,7 +2423,7 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -2676,9 +2676,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.7.0" +version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f" +checksum = "8c9c992b02b5b4c94ea26e32fe5bccb7aa7d9f390ab5c1221ff895bc7ea8b652" dependencies = [ "equivalent", "hashbrown 0.15.0", @@ -2712,7 +2712,7 @@ checksum = "c34819042dc3d3971c46c2190835914dfbe0c3c13f61449b2997f4e9722dfa60" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -2976,7 +2976,7 @@ checksum = "b01f197a15988fb5b2ec0a5a9800c97e70771499c456ad757d63b3c5e9b96e75" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -3213,7 +3213,7 @@ dependencies = [ "cfg_aliases 0.1.1", "codespan-reporting", "hexf-parse", - "indexmap 2.7.0", + "indexmap 2.7.1", "log", "rustc-hash 1.1.0", "spirv", @@ -3370,7 +3370,7 @@ checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -3439,10 +3439,10 @@ version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af1844ef2428cc3e1cb900be36181049ef3d3193c63e43026cfe202983b27a56" dependencies = [ - "proc-macro-crate 3.2.0", + "proc-macro-crate 1.3.1", "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -3953,7 +3953,7 @@ checksum = "3c0f5fad0874fc7abcd4d750e76917eaebbecaa2c20bde22e1dbeeba8beb758c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -4080,7 +4080,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "64d1ec885c64d0457d564db4ec299b2dae3f9c02808b8ad9c3a089c591b18033" dependencies = [ "proc-macro2", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -4143,9 +4143,9 @@ checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068" [[package]] name = "proc-macro2" -version = "1.0.92" +version = "1.0.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" +checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99" dependencies = [ "unicode-ident", ] @@ -4166,7 +4166,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a65f2e60fbf1063868558d69c6beacf412dc755f9fc020f514b7955fc914fe30" dependencies = [ "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -4492,7 +4492,7 @@ dependencies = [ "proc-macro2", "quote", "rust-embed-utils", - "syn 2.0.95", + "syn 2.0.96", "walkdir", ] @@ -4535,7 +4535,7 @@ dependencies = [ "serde", "serde_json", "serde_yml", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -4672,7 +4672,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09c024468a378b7e36765cd36702b7a90cc3cba11654f6685c8f233408e89e92" dependencies = [ "dyn-clone", - "indexmap 2.7.0", + "indexmap 2.7.1", "schemars_derive", "serde", "serde_json", @@ -4687,7 +4687,7 @@ dependencies = [ "proc-macro2", "quote", "serde_derive_internals", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -4766,7 +4766,7 @@ checksum = "de523f781f095e28fa605cdce0f8307e451cc0fd14e2eb4cd2e98a355b147766" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -4777,7 +4777,7 @@ checksum = "18d26a20a969b9e3fdf2fc2d9f21eda6c40e2de84c9408bb5d3b05d499aae711" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -4795,7 +4795,7 @@ version = "1.0.132" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03" dependencies = [ - "indexmap 2.7.0", + "indexmap 2.7.1", "itoa 1.0.11", "memchr", "ryu", @@ -4810,7 +4810,7 @@ checksum = "6c64451ba24fc7a6a2d60fc75dd9c83c90903b19028d4eff35e88fc1e86564e9" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -4828,7 +4828,7 @@ version = "0.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48e76bab63c3fd98d27c17f9cbce177f64a91f5e69ac04cafe04e1bb25d1dc3c" dependencies = [ - "indexmap 2.7.0", + "indexmap 2.7.1", "itoa 1.0.11", "libyml", "log", @@ -5014,7 +5014,7 @@ dependencies = [ "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -5155,7 +5155,7 @@ dependencies = [ "proc-macro2", "quote", "rustversion", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -5301,9 +5301,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.95" +version = "2.0.96" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46f71c0377baf4ef1cc3e3402ded576dccc315800fbc62dfc7fe04b009773b4a" +checksum = "d5d0adab1ae378d7f53bdebc67a39f1f151407ef230f0ce2883572f5d8985c80" dependencies = [ "proc-macro2", "quote", @@ -5359,7 +5359,7 @@ checksum = "f4e16beb8b2ac17db28eab8bca40e62dbfbb34c0fcdc6d9826b11b7b5d047dfd" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -5418,11 +5418,11 @@ dependencies = [ [[package]] name = "thiserror" -version = "2.0.9" +version = "2.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f072643fd0190df67a8bab670c20ef5d8737177d6ac6b2e9a236cb096206b2cc" +checksum = "d452f284b73e6d76dd36758a0c8684b1d5be31f92b89d07fd5822175732206fc" dependencies = [ - "thiserror-impl 2.0.9", + "thiserror-impl 2.0.11", ] [[package]] @@ -5433,18 +5433,18 @@ checksum = "b08be0f17bd307950653ce45db00cd31200d82b624b36e181337d9c7d92765b5" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] name = "thiserror-impl" -version = "2.0.9" +version = "2.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b50fa271071aae2e6ee85f842e2e28ba8cd2c5fb67f11fcb1fd70b276f9e7d4" +checksum = "26afc1baea8a989337eeb52b6e72a039780ce45c3edfcc9c5b9d112feeb173c2" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -5578,7 +5578,7 @@ version = "0.19.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" dependencies = [ - "indexmap 2.7.0", + "indexmap 2.7.1", "serde", "serde_spanned", "toml_datetime", @@ -5591,7 +5591,7 @@ version = "0.20.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70f427fce4d84c72b5b732388bf4a9f4531b53f74e2887e3ecb2481f68f66d81" dependencies = [ - "indexmap 2.7.0", + "indexmap 2.7.1", "toml_datetime", "winnow 0.5.40", ] @@ -5602,7 +5602,7 @@ version = "0.22.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5" dependencies = [ - "indexmap 2.7.0", + "indexmap 2.7.1", "serde", "serde_spanned", "toml_datetime", @@ -5628,7 +5628,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -6024,7 +6024,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", "wasm-bindgen-shared", ] @@ -6046,7 +6046,7 @@ checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -6218,7 +6218,7 @@ checksum = "1d228f15bba3b9d56dde8bddbee66fa24545bd17b48d5128ccf4a8742b18e431" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -6309,7 +6309,7 @@ checksum = "2bbd5b46c938e506ecbce286b6628a02171d56153ba733b6c741fc627ec9579b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -6320,7 +6320,7 @@ checksum = "053c4c462dc91d3b1504c6fe5a726dd15e216ba718e84a0e46a88fbe5ded3515" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -6604,9 +6604,9 @@ dependencies = [ [[package]] name = "wry" -version = "0.48.0" +version = "0.48.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e644bf458e27b11b0ecafc9e5633d1304fdae82baca1d42185669752fe6ca4f" +checksum = "a2e33c08b174442ff80d5c791020696f9f8b4e4a87b8cfc7494aad6167ec44e1" dependencies = [ "base64", "block2", @@ -6634,7 +6634,7 @@ dependencies = [ "sha2", "soup3", "tao-macros", - "thiserror 2.0.9", + "thiserror 2.0.11", "url", "webkit2gtk", "webkit2gtk-sys", @@ -6863,7 +6863,7 @@ dependencies = [ "proc-macro-crate 3.2.0", "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", "zvariant_utils 2.1.0", ] @@ -6876,7 +6876,7 @@ dependencies = [ "proc-macro-crate 3.2.0", "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", "zbus_names 4.1.0", "zvariant 5.1.0", "zvariant_utils 3.0.2", @@ -6929,7 +6929,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -6949,7 +6949,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -7014,7 +7014,7 @@ dependencies = [ "proc-macro-crate 3.2.0", "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", "zvariant_utils 2.1.0", ] @@ -7027,7 +7027,7 @@ dependencies = [ "proc-macro-crate 3.2.0", "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", "zvariant_utils 3.0.2", ] @@ -7039,7 +7039,7 @@ checksum = "c51bcff7cc3dbb5055396bcf774748c3dab426b4b8659046963523cee4808340" dependencies = [ "proc-macro2", "quote", - "syn 2.0.95", + "syn 2.0.96", ] [[package]] @@ -7052,6 +7052,6 @@ dependencies = [ "quote", "serde", "static_assertions", - "syn 2.0.95", + "syn 2.0.96", "winnow 0.6.20", ] diff --git a/crates/story/src/input_story.rs b/crates/story/src/input_story.rs index a2e1c4ff..d321a5cf 100644 --- a/crates/story/src/input_story.rs +++ b/crates/story/src/input_story.rs @@ -127,7 +127,7 @@ impl InputStory { NumberInput::new(cx) .placeholder("Unsized Integer Number Input", cx) .pattern(Regex::new(r"^\d+$").unwrap(), cx) - .small(cx) + .small() }); cx.subscribe(&number_input2, Self::on_number_input2_event) diff --git a/crates/ui/src/input/input.rs b/crates/ui/src/input/input.rs index 10d032cf..b334dee4 100644 --- a/crates/ui/src/input/input.rs +++ b/crates/ui/src/input/input.rs @@ -1449,9 +1449,9 @@ impl Render for TextInput { .rounded(px(cx.theme().radius)) .when(cx.theme().shadow, |this| this.shadow_sm()) .when(focused, |this| this.outline(cx)) - .when(prefix.is_none(), |this| this.input_pl(self.size)) - .when(suffix.is_none(), |this| this.input_pr(self.size)) }) + .when(prefix.is_none(), |this| this.input_pl(self.size)) + .when(suffix.is_none(), |this| this.input_pr(self.size)) .children(prefix) .gap_1() .items_center() diff --git a/crates/ui/src/input/number_input.rs b/crates/ui/src/input/number_input.rs index f098f72b..d22eef6c 100644 --- a/crates/ui/src/input/number_input.rs +++ b/crates/ui/src/input/number_input.rs @@ -1,7 +1,7 @@ use gpui::{ - actions, AppContext, EventEmitter, FocusHandle, FocusableView, InteractiveElement, IntoElement, - KeyBinding, ParentElement, Render, SharedString, Styled, Subscription, View, ViewContext, - VisualContext, + actions, px, AppContext, EventEmitter, FocusHandle, FocusableView, InteractiveElement, + IntoElement, KeyBinding, ParentElement, Pixels, Render, SharedString, Styled, Subscription, + View, ViewContext, VisualContext, }; use regex::Regex; @@ -10,7 +10,7 @@ use crate::{ h_flex, input::{InputEvent, TextInput}, prelude::FluentBuilder, - ActiveTheme, IconName, Sizable, Size, StyledExt, + ActiveTheme, IconName, Sizable, Size, StyleSized, StyledExt, }; actions!(number_input, [Increment, Decrement]); @@ -26,7 +26,9 @@ pub fn init(cx: &mut AppContext) { pub struct NumberInput { input: View, + size: Size, _subscriptions: Vec, + _synced_size: bool, } impl NumberInput { @@ -42,6 +44,8 @@ impl NumberInput { Self { input, + size: Size::default(), + _synced_size: false, _subscriptions, } } @@ -56,6 +60,11 @@ impl NumberInput { self } + pub fn set_size(&mut self, size: Size, cx: &mut ViewContext) { + self.size = size; + self.sync_size_to_input_if_needed(cx); + } + pub fn set_placeholder(&self, text: impl Into, cx: &mut ViewContext) { self.input.update(cx, |input, _| { input.set_placeholder(text); @@ -67,23 +76,6 @@ impl NumberInput { self } - pub fn set_size(self, size: Size, cx: &mut ViewContext) -> Self { - self.input.update(cx, |input, cx| input.set_size(size, cx)); - self - } - - pub fn small(self, cx: &mut ViewContext) -> Self { - self.set_size(Size::Small, cx) - } - - pub fn xsmall(self, cx: &mut ViewContext) -> Self { - self.set_size(Size::XSmall, cx) - } - - pub fn large(self, cx: &mut ViewContext) -> Self { - self.set_size(Size::Large, cx) - } - pub fn set_value(&self, text: impl Into, cx: &mut ViewContext) { self.input.update(cx, |input, cx| input.set_text(text, cx)) } @@ -94,18 +86,18 @@ impl NumberInput { } pub fn increment(&mut self, cx: &mut ViewContext) { - self.handle_increment(&Increment, cx); + self.on_action_increment(&Increment, cx); } pub fn decrement(&mut self, cx: &mut ViewContext) { - self.handle_decrement(&Decrement, cx); + self.on_action_decrement(&Decrement, cx); } - fn handle_increment(&mut self, _: &Increment, cx: &mut ViewContext) { + fn on_action_increment(&mut self, _: &Increment, cx: &mut ViewContext) { self.on_step(StepAction::Increment, cx); } - fn handle_decrement(&mut self, _: &Decrement, cx: &mut ViewContext) { + fn on_action_decrement(&mut self, _: &Decrement, cx: &mut ViewContext) { self.on_step(StepAction::Decrement, cx); } @@ -116,6 +108,14 @@ impl NumberInput { cx.emit(NumberInputEvent::Step(action)); } + + fn sync_size_to_input_if_needed(&mut self, cx: &mut ViewContext) { + if !self._synced_size { + self.input + .update(cx, |input, cx| input.set_size(self.size, cx)); + self._synced_size = true; + } + } } impl FocusableView for NumberInput { @@ -135,18 +135,30 @@ pub enum NumberInputEvent { } impl EventEmitter for NumberInput {} - +impl Sizable for NumberInput { + fn with_size(mut self, size: impl Into) -> Self { + self.size = size.into(); + self + } +} impl Render for NumberInput { fn render(&mut self, cx: &mut ViewContext) -> impl IntoElement { let focused = self.input.focus_handle(cx).is_focused(cx); + // Sync size to input at first. + self.sync_size_to_input_if_needed(cx); + const BUTTON_OFFSET: Pixels = px(-3.); + let btn_size = match self.size { + Size::XSmall | Size::Small => Size::XSmall, + _ => Size::Small, + }; + h_flex() .key_context(KEY_CONTENT) - .on_action(cx.listener(Self::handle_increment)) - .on_action(cx.listener(Self::handle_decrement)) + .on_action(cx.listener(Self::on_action_increment)) + .on_action(cx.listener(Self::on_action_decrement)) .flex_1() - .px_1() - .gap_x_3() + .input_size(self.size) .bg(cx.theme().background) .border_color(cx.theme().input) .border_1() @@ -155,7 +167,8 @@ impl Render for NumberInput { .child( Button::new("minus") .ghost() - .xsmall() + .with_size(btn_size) + .ml(BUTTON_OFFSET) .icon(IconName::Minus) .on_click(cx.listener(|this, _, cx| this.on_step(StepAction::Decrement, cx))), ) @@ -163,7 +176,8 @@ impl Render for NumberInput { .child( Button::new("plus") .ghost() - .xsmall() + .with_size(btn_size) + .mr(BUTTON_OFFSET) .icon(IconName::Plus) .on_click(cx.listener(|this, _, cx| this.on_step(StepAction::Increment, cx))), )