Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

menu: Add scrollable support to menu. #310

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions crates/story/src/popup_story.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use gpui::{
actions, div, impl_actions, px, AnchorCorner, AppContext, DismissEvent, Element, EventEmitter,
FocusHandle, FocusableView, InteractiveElement, IntoElement, KeyBinding, MouseButton,
MouseDownEvent, ParentElement as _, Render, Styled as _, View, ViewContext, VisualContext,
WindowContext,
MouseDownEvent, ParentElement as _, Render, SharedString, Styled as _, View, ViewContext,
VisualContext, WindowContext,
};
use serde::Deserialize;
use ui::{
Expand Down Expand Up @@ -289,6 +289,20 @@ impl Render for PopupStory {
})
}),
)
.child(
Button::new("popup-menu-11112")
.label("Scrollable Menu")
.popup_menu(move |this, _| {
let mut this = this.scrollable();
for i in 0..100 {
this = this.menu(
SharedString::from(format!("Item {}", i)),
Box::new(Info(i)),
)
}
this
}),
)
.child(self.message.clone()),
)
.child("Right click to open ContextMenu")
Expand Down
10 changes: 10 additions & 0 deletions crates/ui/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ impl Disableable for Button {
}

impl Selectable for Button {
fn element_id(&self) -> &ElementId {
&self.id
}

fn selected(mut self, selected: bool) -> Self {
self.selected = selected;
self
Expand Down Expand Up @@ -305,6 +309,12 @@ impl ParentElement for Button {
}
}

impl InteractiveElement for Button {
fn interactivity(&mut self) -> &mut gpui::Interactivity {
self.base.interactivity()
}
}

impl RenderOnce for Button {
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
let style: ButtonStyle = self.style;
Expand Down
4 changes: 4 additions & 0 deletions crates/ui/src/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ impl Disableable for Checkbox {
}

impl Selectable for Checkbox {
fn element_id(&self) -> &ElementId {
&self.id
}

fn selected(self, selected: bool) -> Self {
self.checked(selected)
}
Expand Down
9 changes: 8 additions & 1 deletion crates/ui/src/list/list_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{h_flex, theme::ActiveTheme, Disableable, Icon, IconName, Selectable,

#[derive(IntoElement)]
pub struct ListItem {
id: ElementId,
base: Stateful<Div>,
disabled: bool,
selected: bool,
Expand All @@ -23,8 +24,10 @@ pub struct ListItem {

impl ListItem {
pub fn new(id: impl Into<ElementId>) -> Self {
let id: ElementId = id.into();
Self {
base: h_flex().id(id.into()).gap_x_1().py_1().px_2().text_base(),
id: id.clone(),
base: h_flex().id(id).gap_x_1().py_1().px_2().text_base(),
disabled: false,
selected: false,
confirmed: false,
Expand Down Expand Up @@ -98,6 +101,10 @@ impl Disableable for ListItem {
}

impl Selectable for ListItem {
fn element_id(&self) -> &ElementId {
&self.id
}

fn selected(mut self, selected: bool) -> Self {
self.selected = selected;
self
Expand Down
4 changes: 2 additions & 2 deletions crates/ui/src/popover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use gpui::{
AppContext, Bounds, DismissEvent, DispatchPhase, Element, ElementId, EventEmitter, FocusHandle,
FocusableView, GlobalElementId, Hitbox, InteractiveElement as _, IntoElement, KeyBinding,
LayoutId, ManagedView, MouseButton, MouseDownEvent, ParentElement, Pixels, Point, Render,
Style, Styled, View, ViewContext, VisualContext, WindowContext,
SharedString, Style, Styled, View, ViewContext, VisualContext, WindowContext,
};
use std::{cell::RefCell, rc::Rc};

Expand Down Expand Up @@ -131,7 +131,7 @@ where
}

fn render_trigger(&mut self, is_open: bool, cx: &mut WindowContext) -> impl IntoElement {
let base = div().id("popover-trigger");
let base = div().id(SharedString::from(format!("{}-trigger", self.id)));

if self.trigger.is_none() {
return base;
Expand Down
Loading
Loading