Skip to content

Commit

Permalink
✨ Add property to hide widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
ronisbr committed Mar 29, 2024
1 parent d6c3f3d commit be52111
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/macros/widgets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ macro widget(ex)
# Mark if the widget needs to be update.
:(update_needed::Bool = true)

# Indicate that the widget is hidden.
:(hidden::Bool = false)

# Default signals.
:(@signal focus_acquired)
:(@signal focus_lost)
Expand Down
17 changes: 12 additions & 5 deletions src/widgets/container.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ function redraw!(container::WidgetContainer)
# Redraw all the widgets in the container if necessary.
if update_needed
for widget in widgets
redraw!(widget)
widget.hidden || redraw!(widget)
end
end

Expand Down Expand Up @@ -355,7 +355,7 @@ function move_focus_to_widget!(container::WidgetContainer, widget::Widget)
id = findfirst(w -> w === widget, container.widgets)

if !isnothing(id)
if request_focus!(widget)
if request_focus!(widget) && !widget.hidden
_change_focused_widget!(container, id)

# We also need to make this container in focus if it is inside
Expand Down Expand Up @@ -388,7 +388,10 @@ function get_focused_widget(container::WidgetContainer)
return nothing
end

return widgets[focused_widget_id]
# If the widget is hidden, we must report that we have no focus.
focused_widget = widgets[focused_widget_id]
focused_widget.hidden && return nothing
return focused_widget
end
end

Expand Down Expand Up @@ -544,7 +547,9 @@ function _search_next_widget_to_focus(
candidate_widget = widgets[focus_candidate_id]

# Check if the candidate can accept the focus.
can_accept_focus(candidate_widget) && return focus_candidate_id
if can_accept_focus(candidate_widget) && !candidate_widget.hidden
return focus_candidate_id
end

num_tries += 1

Expand Down Expand Up @@ -592,7 +597,9 @@ function _search_previous_widget_to_focus(
candidate_widget = widgets[focus_candidate_id]

# Check if the candidate can accept the focus.
can_accept_focus(candidate_widget) && return focus_candidate_id
if can_accept_focus(candidate_widget) && !candidate_widget.hidden
return focus_candidate_id
end

num_tries += 1

Expand Down
28 changes: 26 additions & 2 deletions src/widgets/widgets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#
############################################################################################

export create_widget, destroy_widget_buffer!, get_buffer, get_parent, request_update!, move_focus_to_widget
export create_widget, destroy_widget_buffer!, get_buffer, get_parent, hide!
export move_focus_to_widget, request_update!, unhide!

"""
create_widget_buffer!(widget::Widget) -> Nothing
Expand Down Expand Up @@ -135,6 +136,18 @@ function has_focus(widget::Widget)
end
end

"""
hide!(widget::Widget) -> Nothing
Hide `widget`, meaning that it will still be in the parent container but it will not be
drawn or receive focus.
"""
function hide!(widget::Widget)
widget.hidden = true
request_update!(widget)
return nothing
end

"""
move_focus_to_widget(widget::Widget) -> Nothing
Expand Down Expand Up @@ -169,14 +182,25 @@ If `force` is `true`, then the widget will be updated even if it is not needed.
"""
function update!(widget::Widget; force::Bool = false)
if widget.update_needed || force
redraw!(widget)
widget.hidden || redraw!(widget)
widget.update_needed = false
return true
else
return false
end
end

"""
unhide!(widget::Widget) -> Nothing
Unhide `widget`, meaning that it will be drawn in its parent buffer.
"""
function unhide!(widget::Widget)
widget.hidden = false
request_update!(widget)
return nothing
end

"""
update_widget_layout!(widget::Widget; force::Bool = true) -> Nothing
Expand Down

0 comments on commit be52111

Please sign in to comment.