Encapsulating commands as 'actions' #380
Replies: 11 comments 8 replies
-
Very bad code example, but something like... // defining the application
auto copyAction = my_app.defineAction("Copy");
copyAction->bind_short_cut("ctrl-C");
my_menu.add_action("Edit/Copy", copyAction);
// elsewhere
myWidget->subscribe_to_action(copyAction, [=]() {myWidget->copy_to_clipboard();} |
Beta Was this translation helpful? Give feedback.
-
I think this can be done using the new model API (just implemented recently). There can be an app model that elements can subscribe too. It's a generic/modern c++ twist to the 'event-bus' or 'signal slots'. There's an example with lots of comments. |
Beta Was this translation helpful? Give feedback.
-
The model is a nice way to decouple state from the UI code, but it doesn't solve the problem of widget implementors having to reimplement code to handle commands, nor of making sure seperate command triggers carry out the same actions. For example, I'm currently writing a complex element that needs to take cut/copy/paste commands. To do that, I have to re-implement all the key handling found in text.cpp, with all the associated problems of code duplication. Actions allow seperate UI types to behave uniformly and allow for richer behaviour. |
Beta Was this translation helpful? Give feedback.
-
Sure, I get your point. But it's just a matter of protocol. Say we have a generic app_model that is used as a starting point where such cut/copy/paste commands are available as a starting point, and probably some more such as quit, etc. Then, we can use this as a common hook into elements, including |
Beta Was this translation helpful? Give feedback.
-
Take note too that the philosophy behind |
Beta Was this translation helpful? Give feedback.
-
I'd argue that that is incorrect (or at least incomplete). The Model represents the underlying data (state) and the logic that operates on that data (actions) at a high level, and decoupled from the UI view. The clipboard, for example, can be part of the model and so is the logic (actions) that operate on the clipboard such as cut/copy and paste. |
Beta Was this translation helpful? Give feedback.
-
It affects the selection, the type of which is application-specific, but can be abstracted out. For example, in a recent project I worked on, the selection is a sub-range of items in a list.
Exactly, and the operation is the business logic. |
Beta Was this translation helpful? Give feedback.
-
This is all a matter of design, can you agree? The model is already an abstraction and encapsulation that can handle what you call "commands". I see no reason to add another abstraction. State cannot exist alone without the business logic surrounding it. I'd even argue that there can exist a purely functional stateless model (e.g. one that is based purely on functions). |
Beta Was this translation helpful? Give feedback.
-
I don't think so. There's the MVC and also the MVP design patterns in GUI design (Model-View-Controller, and Model-View-Presenter, respectively). I've worked on both designs, but I lean towards MVP. In both design patterns, IMO, the Model is the central abstraction. Commands? Nada. The closest is perhaps the implementation of the controller in the MVC design, like the event bus or the signal/slot. But that is not a key player and does not exist in MVP. Those are simply implementation aspects.
If you are willing to move towards a more constructive and productive discussion, instead of pushing me to accept other UI toolkit design, I can present and discuss various ways. |
Beta Was this translation helpful? Give feedback.
-
Appologies for pushing to hard. How do you think one should go about encapsulating that kind of behaviour so that a developer does not have to reimplement ctrl-C/V/X for each element they create while also responding to the menu comands that do the equivalent? Similarly for other common interactions, eg: find, find/replace, delete and so on? |
Beta Was this translation helpful? Give feedback.
-
Those are splendid questions. And you are right in thinking that these should be implemented once. I'll share some ideas later... BTW, if you want real-time discussions, I suggest continuing over discord channel to flesh out details. |
Beta Was this translation helpful? Give feedback.
-
Many toolkits encapsulate commands that can be triggered by multiple means as 'actions'. For example the ‘copy’ command can be triggered by 'ctrl-C’ or by choosing the ‘copy’ item from a menu. Currently implementors of a new element have to recreate all the copy/paste etc.. logic, as well as dealing with menus. If commands were encapsulated as actions, elements simply subscribe to an action. An action can be triggered by a menu, a keyboard shortcut or even another element. When an element has focus and an action is triggered, the focused element receives notification of the action via a signal/slot, virtual or callback mechanism. Custom actions can also be defined and subscribed to by application and element developers.
Beta Was this translation helpful? Give feedback.
All reactions