Skip to content

Commit d399791

Browse files
authored
fix: font weight stepper delta=100 (#372)
1 parent d090c7c commit d399791

3 files changed

Lines changed: 127 additions & 21 deletions

File tree

appium/test_integer.py

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
from appium.webdriver.webdriver import WebDriver
2+
from selenium.webdriver.common.keys import Keys
23
from util.button import get_undo_redo
3-
from util.config import read_global_config
4+
from util.config import read_global_config, read_theme_config
45
from util.integer import (
56
click_stepper_decrement,
67
click_stepper_increment,
78
get_integer_value,
89
)
10+
from util.key import press
911
from util.message import (
1012
BUTTON_SHOULD_BE_DISABLED,
1113
BUTTON_SHOULD_BE_ENABLED,
1214
CHANGE_NOT_SAVED,
1315
UI_NOT_UPDATED,
1416
)
15-
from util.window import find_element_by_id, open_global_config
17+
from util.string import get_string_value
18+
from util.window import find_element_by_id, open_global_config, open_theme_config
1619

1720
INTEGER_ID = "DefaultPageSize"
1821
INT_MAX = 10
22+
FONT_SECTION = "Font"
23+
FONT_WEIGHT_ID = "TextFontWeight"
1924

2025

2126
def test_default_page_size(driver: WebDriver, app: str) -> None:
@@ -61,6 +66,14 @@ def read_config_value() -> str:
6166
assert final_value == initial_value, UI_NOT_UPDATED
6267
assert read_config_value() == str(final_value), CHANGE_NOT_SAVED
6368

69+
# Test non-numeric input
70+
field.click()
71+
field.clear()
72+
field.send_keys("x")
73+
assert get_string_value(field) == "x", UI_NOT_UPDATED
74+
press(driver, [Keys.ENTER])
75+
assert get_integer_value(field) == initial_value, UI_NOT_UPDATED
76+
6477
# Test input validation: Enter value exceeding max (10)
6578
field.click()
6679
field.clear()
@@ -78,3 +91,49 @@ def read_config_value() -> str:
7891
reset_value = get_integer_value(field)
7992
assert reset_value == initial_value, UI_NOT_UPDATED
8093
assert read_config_value() == str(initial_value), CHANGE_NOT_SAVED
94+
95+
96+
def test_font_weight(driver: WebDriver, app: str) -> None:
97+
open_theme_config(driver)
98+
find_element_by_id(driver, FONT_SECTION).click()
99+
100+
def read_config_value() -> str:
101+
cfg = read_theme_config(app)
102+
return cfg[FONT_SECTION][FONT_WEIGHT_ID]
103+
104+
field = find_element_by_id(driver, FONT_WEIGHT_ID)
105+
stepper = find_element_by_id(driver, f"{FONT_WEIGHT_ID}_stepper")
106+
107+
def set_value(value: str) -> None:
108+
field.click()
109+
field.clear()
110+
field.send_keys(value)
111+
press(driver, [Keys.ENTER])
112+
113+
def assert_value(value: int) -> None:
114+
assert get_integer_value(field) == value, UI_NOT_UPDATED
115+
assert read_config_value() == str(value), CHANGE_NOT_SAVED
116+
117+
set_value("950")
118+
assert_value(950)
119+
120+
click_stepper_increment(driver, stepper)
121+
assert_value(1000)
122+
123+
click_stepper_decrement(driver, stepper)
124+
assert_value(900)
125+
126+
click_stepper_decrement(driver, stepper)
127+
assert_value(800)
128+
129+
set_value("50")
130+
assert_value(50)
131+
132+
click_stepper_decrement(driver, stepper)
133+
assert_value(1)
134+
135+
click_stepper_increment(driver, stepper)
136+
assert_value(100)
137+
138+
click_stepper_increment(driver, stepper)
139+
assert_value(200)

src/config/IntegerView.swift

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,34 @@ struct IntegerView: OptionViewProtocol {
3030
)
3131
}
3232

33+
private var isFontWeight: Bool {
34+
data["FontWeight"] as? String == "True"
35+
}
36+
37+
private func incrementValue(_ value: Int, maxValue: Int?) -> Int {
38+
let next = isFontWeight ? ((value / 100) + 1) * 100 : value + 1
39+
return min(next, maxValue ?? next)
40+
}
41+
42+
private func decrementValue(_ value: Int, minValue: Int?) -> Int {
43+
let previous = isFontWeight ? ((value - 1) / 100) * 100 : value - 1
44+
return max(previous, minValue ?? previous)
45+
}
46+
47+
private func canIncrement(maxValue: Int?) -> Bool {
48+
guard let maxValue = maxValue else {
49+
return true
50+
}
51+
return number < maxValue
52+
}
53+
54+
private func canDecrement(minValue: Int?) -> Bool {
55+
guard let minValue = minValue else {
56+
return true
57+
}
58+
return number > minValue
59+
}
60+
3361
var body: some View {
3462
let minValue = Int(data["IntMin"] as? String ?? "")
3563
let maxValue = Int(data["IntMax"] as? String ?? "")
@@ -49,7 +77,16 @@ struct IntegerView: OptionViewProtocol {
4977
}
5078
if #available(macOS 26.0, *) {
5179
let stepperId = option + "_stepper"
52-
if let minValue = minValue, let maxValue = maxValue {
80+
if isFontWeight {
81+
Stepper {
82+
} onIncrement: {
83+
number = incrementValue(number, maxValue: maxValue)
84+
} onDecrement: {
85+
number = decrementValue(number, minValue: minValue)
86+
}
87+
.accessibilityIdentifier(stepperId)
88+
.disabled(!canIncrement(maxValue: maxValue) && !canDecrement(minValue: minValue))
89+
} else if let minValue = minValue, let maxValue = maxValue {
5390
Stepper(
5491
value: $number,
5592
in: minValue...maxValue,
@@ -59,25 +96,25 @@ struct IntegerView: OptionViewProtocol {
5996
} else {
6097
Stepper {
6198
} onIncrement: {
62-
number += 1
99+
number = incrementValue(number, maxValue: maxValue)
63100
} onDecrement: {
64-
number -= 1
101+
number = decrementValue(number, minValue: minValue)
65102
}
66103
.accessibilityIdentifier(stepperId)
67104
}
68105
} else {
69106
// Stepper is too narrow.
70107
HStack(spacing: 0) {
71108
Button {
72-
number -= 1
109+
number = decrementValue(number, minValue: minValue)
73110
} label: {
74111
Image(systemName: "minus")
75-
}.disabled(minValue != nil && number <= minValue ?? 0)
112+
}.disabled(!canDecrement(minValue: minValue))
76113
Button {
77-
number += 1
114+
number = incrementValue(number, maxValue: maxValue)
78115
} label: {
79116
Image(systemName: "plus")
80-
}.disabled(maxValue != nil && number >= maxValue ?? 0)
117+
}.disabled(!canIncrement(maxValue: maxValue))
81118
}
82119
}
83120
}

webpanel/webpanel.h

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ struct PluginAnnotation {
7070
}
7171
};
7272

73+
struct FontWeightAnnotation {
74+
bool skipDescription() { return false; }
75+
bool skipSave() { return false; }
76+
void dumpDescription(RawConfig &config) {
77+
config.setValueByPath("FontWeight", "True");
78+
}
79+
};
80+
7381
FCITX_CONFIGURATION(
7482
BasicConfig,
7583
Option<bool> followCaret{this, "FollowCaret", _("Follow caret"), false};
@@ -266,39 +274,41 @@ FCITX_CONFIGURATION(
266274

267275
using FontFamilyOption =
268276
OptionWithAnnotation<std::vector<std::string>, FontAnnotation>;
277+
using FontWeightOption =
278+
Option<int, IntConstrain, DefaultMarshaller<int>, FontWeightAnnotation>;
269279

270280
FCITX_CONFIGURATION(
271281
FontConfig,
272282
FontFamilyOption textFontFamily{
273283
this, "TextFontFamily", _("Text font family"), {""}};
274284
Option<int, IntConstrain> textFontSize{
275285
this, "TextFontSize", _("Text font size"), 16, IntConstrain(4, 100)};
276-
Option<int, IntConstrain> textFontWeight{this, "TextFontWeight",
277-
_("Text font weight"), 400,
278-
IntConstrain(1, 1000)};
286+
FontWeightOption textFontWeight{this, "TextFontWeight",
287+
_("Text font weight"), 400,
288+
IntConstrain(1, 1000)};
279289
FontFamilyOption labelFontFamily{
280290
this, "LabelFontFamily", _("Label font family"), {""}};
281291
Option<int, IntConstrain> labelFontSize{
282292
this, "LabelFontSize", _("Label font size"), 12, IntConstrain(4, 100)};
283-
Option<int, IntConstrain> labelFontWeight{this, "LabelFontWeight",
284-
_("Label font weight"), 400,
285-
IntConstrain(1, 1000)};
293+
FontWeightOption labelFontWeight{this, "LabelFontWeight",
294+
_("Label font weight"), 400,
295+
IntConstrain(1, 1000)};
286296
FontFamilyOption commentFontFamily{
287297
this, "CommentFontFamily", _("Comment font family"), {""}};
288298
Option<int, IntConstrain> commentFontSize{this, "CommentFontSize",
289299
_("Comment font size"), 12,
290300
IntConstrain(4, 100)};
291-
Option<int, IntConstrain> commentFontWeight{this, "CommentFontWeight",
292-
_("Comment font weight"), 400,
293-
IntConstrain(1, 1000)};
301+
FontWeightOption commentFontWeight{this, "CommentFontWeight",
302+
_("Comment font weight"), 400,
303+
IntConstrain(1, 1000)};
294304
FontFamilyOption preeditFontFamily{
295305
this, "PreeditFontFamily", _("Preedit font family"), {""}};
296306
Option<int, IntConstrain> preeditFontSize{this, "PreeditFontSize",
297307
_("Preedit font size"), 16,
298308
IntConstrain(4, 100)};
299-
Option<int, IntConstrain> preeditFontWeight{this, "PreeditFontWeight",
300-
_("Preedit font weight"), 400,
301-
IntConstrain(1, 1000)};
309+
FontWeightOption preeditFontWeight{this, "PreeditFontWeight",
310+
_("Preedit font weight"), 400,
311+
IntConstrain(1, 1000)};
302312
ExternalOption userFontDir{this, "UserFontDir", _("User font dir"), ""};
303313
ExternalOption systemFontDir{this, "SystemFontDir", _("System font dir"),
304314
""};);

0 commit comments

Comments
 (0)