Skip to content

Commit

Permalink
ContextMenu & SidePane
Browse files Browse the repository at this point in the history
Add 'enabled' property to ContextMenuEntry
Replace ListView with ListView.builder
  • Loading branch information
chesnoksatan committed Jun 24, 2022
1 parent 575dc90 commit 52d57a7
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 64 deletions.
81 changes: 57 additions & 24 deletions lib/widgets/context_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ class ContextSubMenuEntry extends PopupMenuEntry<String> {
/// They can be [ContextMenuEntry], [ContextMenuDivider], [ContextSubMenuEntry] or any other widgets inherited from PopupMenuEntry.
final List<PopupMenuEntry> entries;

final bool enabled;

const ContextSubMenuEntry({
required this.id,
this.leading,
required this.title,
required this.entries,
this.enabled = true,
Key? key,
}) : super(key: key);

Expand All @@ -81,15 +84,18 @@ class _ContextSubMenuEntryState extends State<ContextSubMenuEntry> {
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
// Get current render box of the context widget (ContextSubMenuEntry).
final RenderBox renderBox = context.findRenderObject() as RenderBox;
// Get the position where the submenu should be opened.
final Offset offset =
renderBox.localToGlobal(Offset(renderBox.size.width + 1, -8));

_openContextMenu(context, offset, widget.entries);
},
onTap: widget.enabled
? () {
// Get current render box of the context widget (ContextSubMenuEntry).
final RenderBox renderBox =
context.findRenderObject()! as RenderBox;
// Get the position where the submenu should be opened.
final Offset offset =
renderBox.localToGlobal(Offset(renderBox.size.width + 1, -8));

_openContextMenu(context, offset, widget.entries);
}
: null,
child: Container(
height: widget.height,
padding: const EdgeInsets.symmetric(horizontal: 16),
Expand All @@ -99,8 +105,12 @@ class _ContextSubMenuEntryState extends State<ContextSubMenuEntry> {
IconTheme.merge(
data: IconThemeData(
size: 20,
color:
Theme.of(context).colorScheme.onSurface.withOpacity(0.7),
color: widget.enabled
? Theme.of(context).colorScheme.onSurface.withOpacity(0.7)
: Theme.of(context)
.colorScheme
.onSurface
.withOpacity(0.3),
),
child: widget.leading!,
),
Expand All @@ -110,8 +120,12 @@ class _ContextSubMenuEntryState extends State<ContextSubMenuEntry> {
child: DefaultTextStyle(
style: TextStyle(
fontSize: 16,
color:
Theme.of(context).colorScheme.onSurface.withOpacity(0.7),
color: widget.enabled
? Theme.of(context).colorScheme.onSurface.withOpacity(0.7)
: Theme.of(context)
.colorScheme
.onSurface
.withOpacity(0.3),
),
overflow: TextOverflow.ellipsis,
child: widget.title,
Expand All @@ -120,7 +134,9 @@ class _ContextSubMenuEntryState extends State<ContextSubMenuEntry> {
IconTheme.merge(
data: IconThemeData(
size: 20,
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.7),
color: widget.enabled
? Theme.of(context).colorScheme.onSurface.withOpacity(0.7)
: Theme.of(context).colorScheme.onSurface.withOpacity(0.3),
),
child: const Icon(Icons.chevron_right),
)
Expand Down Expand Up @@ -151,12 +167,15 @@ class ContextMenuEntry extends PopupMenuEntry<String> {
/// Typically a [Text] widget.
final Widget? shortcut;

final bool enabled;

const ContextMenuEntry({
required this.id,
this.leading,
required this.title,
required this.onTap,
this.shortcut,
this.enabled = true,
Key? key,
}) : super(key: key);

Expand All @@ -174,10 +193,12 @@ class _ContextMenuEntryState extends State<ContextMenuEntry> {
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
Navigator.pop(context);
widget.onTap();
},
onTap: widget.enabled
? () {
Navigator.pop(context);
widget.onTap.call();
}
: null,
child: Container(
height: widget.height,
padding: const EdgeInsets.symmetric(horizontal: 16),
Expand All @@ -187,8 +208,12 @@ class _ContextMenuEntryState extends State<ContextMenuEntry> {
IconTheme.merge(
data: IconThemeData(
size: 20,
color:
Theme.of(context).colorScheme.onSurface.withOpacity(0.7),
color: widget.enabled
? Theme.of(context).colorScheme.onSurface.withOpacity(0.7)
: Theme.of(context)
.colorScheme
.onSurface
.withOpacity(0.3),
),
child: widget.leading!,
),
Expand All @@ -198,8 +223,12 @@ class _ContextMenuEntryState extends State<ContextMenuEntry> {
child: DefaultTextStyle(
style: TextStyle(
fontSize: 16,
color:
Theme.of(context).colorScheme.onSurface.withOpacity(0.7),
color: widget.enabled
? Theme.of(context).colorScheme.onSurface.withOpacity(0.7)
: Theme.of(context)
.colorScheme
.onSurface
.withOpacity(0.3),
),
overflow: TextOverflow.ellipsis,
child: widget.title,
Expand All @@ -209,8 +238,12 @@ class _ContextMenuEntryState extends State<ContextMenuEntry> {
DefaultTextStyle(
style: TextStyle(
fontSize: 16,
color:
Theme.of(context).colorScheme.onSurface.withOpacity(0.5),
color: widget.enabled
? Theme.of(context).colorScheme.onSurface.withOpacity(0.5)
: Theme.of(context)
.colorScheme
.onSurface
.withOpacity(0.1),
),
overflow: TextOverflow.ellipsis,
child: widget.shortcut!,
Expand Down
76 changes: 36 additions & 40 deletions lib/widgets/side_pane.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,47 +51,43 @@ class _SidePaneState extends State<SidePane> {
width: 304,
child: Material(
color: Theme.of(context).colorScheme.surface,
child: ListView(
child: ListView.builder(
padding: const EdgeInsets.only(top: 56),
children: widget.destinations
.mapIndexed(
(index, d) => ContextMenu(
entries: [
ContextMenuEntry(
id: 'open',
title: const Text("Open"),
onTap: () => widget.workspace.currentDir =
widget.destinations[index].path,
),
ContextMenuEntry(
id: 'open_in_new_tab',
title: const Text("Open in new tab"),
onTap: () => widget.onNewTab(widget.destinations[index].path),
),
ContextMenuEntry(
id: 'open_in_new_window',
title: const Text("Open in new window"),
onTap: () {},
),
],
child: ListTile(
dense: true,
leading: Icon(widget.destinations[index].icon),
selected: widget.workspace.currentDir ==
widget.destinations[index].path,
selectedTileColor: Theme.of(context)
.colorScheme
.secondary
.withOpacity(0.1),
title: Text(
widget.destinations[index].label,
),
onTap: () => widget.workspace.currentDir =
widget.destinations[index].path,
),
),
)
.toList(),
itemCount: widget.destinations.length,
itemBuilder: (context, index) => ContextMenu(
entries: [
ContextMenuEntry(
id: 'open',
title: const Text("Open"),
onTap: () => widget.workspace.currentDir =
widget.destinations[index].path,
),
ContextMenuEntry(
id: 'open_in_new_tab',
title: const Text("Open in new tab"),
onTap: () => widget.onNewTab(widget.destinations[index].path),
),
ContextMenuEntry(
id: 'open_in_new_window',
title: const Text("Open in new window"),
onTap: () {},
enabled: false,
),
],
child: ListTile(
dense: true,
leading: Icon(widget.destinations[index].icon),
selected: widget.workspace.currentDir ==
widget.destinations[index].path,
selectedTileColor:
Theme.of(context).colorScheme.secondary.withOpacity(0.1),
title: Text(
widget.destinations[index].label,
),
onTap: () =>
widget.workspace.currentDir = widget.destinations[index].path,
),
),
),
),
);
Expand Down
1 change: 1 addition & 0 deletions lib/widgets/table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ class _FilesRowState extends State<_FilesRow> {
title: const Text("Open"),
onTap: () {},
shortcut: const Text("Return"),
enabled: false,
),
ContextMenuEntry(
id: 'open_with',
Expand Down

0 comments on commit 52d57a7

Please sign in to comment.