Repository navigation
0.3 cleanup 3/4: Slider, TextInput, Dropdown and BarChart - #10
Merged
Merged
Conversation
Slider takes (min, max, step=1, *, value, on_change, show_value, font, colors), exposes a clamped, snapped value property and calls on_change(value) only when interaction changes the value. The knob grows on hover and drag, Left/Right/Home/End adjust it while hovered, and the shown value uses the decimals that min and step need. TextInput takes (text="", *, placeholder, width=200, font, colors, border_radius, on_change, on_submit). It has a fixed width, a settable text property and focused flag, calls on_change(text) after each edit, submits on Enter and unfocuses on Escape. combobox.ComboBox becomes dropdown.Dropdown, a standalone widget with value and index properties, on_change(value) on user selection, keyboard navigation, a drawn chevron instead of the arrow asset, and a menu that consumes pointer events while open. Group marks widgets after a consumed MOUSEMOTION as not hovered, so nothing under an open menu stays hovered. BarChart takes keyword-only options plus colors and speed, has a values property whose setter replaces all bars, and set_value(index, value).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #9.
Summary
Third of four stacked PRs for the 0.3.0 cleanup: the input widgets and the chart.
Slider(min, max, step=1, *, value=None, on_change=None, show_value=False, font=None, fill_color, track_color, knob_color, text_color)valueis a property. The setter clamps and snaps tostepwithout float drift (0.1 * 3is0.3) and doesn't fire the callback.on_change(value)fires only when a drag, click or key changes the value.draw(surface, x, y, width=200, height=6)draws a rounded track and fill. The knob grows on hover and while dragging.maxgoes to the last grid value.show_valueformats with the decimals thatminandstepneed (value_text): step 0.1 →0.3, step 0.25 →0.50, integer steps →50. This replaces thestr()check.TextInput(text="", *, placeholder="", width=200, font=None, text_color, placeholder_color, background_color, border_color, focus_color, border_radius=4, on_change=None, on_submit=None)text(setter moves the caret to the end, no callback) andfocused(wasactive) are settable.on_change(text)runs after each edit that changes the text. Enter callson_submit(text)and keeps focus; Escape unfocuses.TEXTINPUT, the custom key repeat, scrolling to the caret, and event priority 1 while focused are unchanged. The text is re-rendered at draw time, so changing colors or the placeholder applies immediately.combobox.ComboBox→dropdown.Dropdown(options, *, index=0, on_change=None, width=None, font=None, color, text_color, highlight_color, outline_color, border_radius)Widget(no Button inheritance). It draws its own box, a chevron drawn withpg.draw.lines, and a menu below with a small shadow. The hovered option useshighlight_colorand the selected one a light shade. Thecombo_box_arrow.pngasset is deleted; no packaging change is needed because uv_build ships the package directory.value(str; settable to an existing option, otherwise ValueError) andindex(settable, IndexError out of range) don't fire callbacks.on_change(value)runs when the user picks a different option.Group.handle_eventstill stops dispatching at the first consumer. ForMOUSEMOTION, it now also setshovered = Falseon every widget after the consumer. Only an open dropdown consumes motion, and only while the pointer is over it. So widgets under the menu lose hover, and the cursor comes from the menu. This was the smallest correct option. The alternative was passing motion to every widget with an extra "occluded" flag, which would change every widget'shandle_event.BarChart(values, *, labels=None, max_value=None, colors=None, font=None, label_color="black", speed=200)set_value(index, value)updates one bar. Thevaluesproperty returns a tuple. Its setter replaces all bars: new bars animate from 0, and automatic colors are regenerated when the count changes. Givencolorsare kept and cycle.max_value/speed.Widget.__init__, so invalid widgets never join a group.Breaking changes
See CHANGELOG.md, "Upgrading from 0.2". In short:
get_value()/current_value/change_value_to()/connect()→value/on_change.fixed_width→width,active→focused,get_text()/set_text()→text,font_color→text_color.combobox.ComboBox→dropdown.Dropdown:elements→options,selected_index→index,add_action→on_change.change_value→set_value,labels_color→label_color, keyword-only options.How tested
uv run ruff check . && uv run ruff format --check . && uv run pytest && uv run pygameuikit-demo --frames 5passes (78 tests). New and updated tests cover:valuessetter (colors, animation from 0, custom colors kept),speed, and validation before joining a groupI also rendered the slider (normal, hover with show_value, step 0.1), the text input (placeholder, focused with text, hovered), the dropdown (closed, open with hover over another widget, dark variant) and the bar chart headlessly and inspected the image. I simulated a demo flow too: selecting a dropdown option fills the text input, and the slider shuffles the chart.