Giter Club home page Giter Club logo

Comments (7)

knopp avatar knopp commented on June 19, 2024 1

Thank you for the snippet. I was able to reproduce the problem. The issue is that NativeShell doesn't detect change from enabled to disabled, so it doesn't update the menu. If you for example modify the menu item title, i.e.

        MenuItem(
            title: controller.selection.isCollapsed ? 'Save_' : 'Save', // change title when section changes
            accelerator: cmdOrCtrl + 's',
            action: controller.selection.isCollapsed ? null : () {},
          ),

it will work as expected. Shouldn't be too difficult to fix.

from nativeshell.

knopp avatar knopp commented on June 19, 2024 1

Should be fixed by 869b4a8. You can try to specify the revision in your pubspec.yaml to see if it works for you.

from nativeshell.

knopp avatar knopp commented on June 19, 2024

This seems like a bug. A minimal reproducible example would help. However if you need to update menu, just store the menu instance (that you set as window menu) and call menu.update(). It is much better that way, as it will only update the parts of platform menu that have actually changed.

I.e.

  late Menu menu;
  @override
  void initState() {
    super.initState();
    menu = Menu(_buildMenu);
    window.setWindowMenu(menu);
    notifier.addListener(_onDocumentOrComposerUpdated);
  }
  void _onDocumentOrComposerUpdated() {
      menu.update();
  }

from nativeshell.

wilsonowilson avatar wilsonowilson commented on June 19, 2024

I'll try to come up with a reproducible sample of the bug.
I just tried menu.update(). It doesn't update the menu for some reason.

from nativeshell.

knopp avatar knopp commented on June 19, 2024

menu.update() definitely should update the menu. If it doesn't for any reason it is a bug. If you can provide any code that I can use to reproduce the problem that'd be great!

from nativeshell.

wilsonowilson avatar wilsonowilson commented on June 19, 2024

Here's something very similar to what I'm doing. The command of interest is the "save" command. If the text controller's selection is collapsed, then its action is null, otherwise, it's set to a callback. Setting a new menu every time works (use that to reproduce the previous issue), but not using menu update.

class TextFieldMenuBuilder extends StatefulWidget {
  const TextFieldMenuBuilder({
    Key? key,
    required this.child,
  }) : super(key: key);

  final Widget child;

  @override
  _TextFieldMenuBuilderState createState() => _TextFieldMenuBuilderState();
}

class _TextFieldMenuBuilderState extends State<TextFieldMenuBuilder> {
  late Menu _menu;
  late final TextEditingController controller;

  @override
  void initState() {
    super.initState();
    controller = TextEditingController();
    controller.addListener(_onTextOrSelectionChanged);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _menu = Menu(_buildMenu);
    Window.of(context).setWindowMenu(_menu);
  }

  void _onTextOrSelectionChanged() {
    _menu.update();
  }

  List<MenuItem> _buildMenu() {
    return [
      if (Platform.isMacOS)
        MenuItem.children(
          title: 'App',
          children: [
            MenuItem.withRole(role: MenuItemRole.hide),
          ],
        ),
      MenuItem.children(
        title: '&File',
        children: [
          MenuItem(title: 'New', accelerator: cmdOrCtrl + 'n', action: null),
          MenuItem.separator(),
          // COMMAND OF INTEREST
          MenuItem(
            title: 'Save',
            accelerator: cmdOrCtrl + 's',
            action: controller.selection.isCollapsed ? null : () {},
          ),
        ],
      ),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: TextField(
        controller: controller,
      ),
    );
  }
}

from nativeshell.

wilsonowilson avatar wilsonowilson commented on June 19, 2024

It works great! Thanks!

from nativeshell.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.