Skip to content

Making maps do things

Leonardo Pessoa edited this page Jul 8, 2022 · 1 revision

So, any map you push to your application will give you instant screen design and edition and navigation between fields that you can use and cast to any format you might need. But do you remember that the last line of our map contained a set of keys associated with actions (PF1 for help, PF3 to go back/exit, and PF6 to save)? Have you tried pushing those keys?

Okay, now you found that this toolkit is not so magical. There are many things you have to do by hand like declaring what pressing those (and maybe other) keys on the user's keyboard will do for each map. But don't worry, that is an easy task as well, and the last one to get any map ready to be used. Our framework provides an attribute called CommandKeyAttribute, with which you can mark a method to be triggered when a specific key (or key combination) is pressed by the user:

   [CommandKey(ConsoleKey.F1)]
   public void ShowHelp()
      => new BookInfoHelpMap().ShowWindow();

   [CommandKey(ConsoleKey.F3)]
   public void DoClose()
      => Close();

Here again the choice for how to handle keys pressed in code is up to you. If you need to use a combination of keys, the CommandKeyAttribute allows you to specify it preceding the key itself using the KeyModifier enumeration.

Moving to another field using ENTER

Returning to the topic we touched a few pages before, some old console applications used to allow navigating through fields using the ENTER key. We chose not to hardcode this in the framework because ENTER might mean different things in the view of different people, organisations or applications, but is also quite simple to implement if necessary (now we'll see how) by capturing the ENTER to a method using the CommandKeyAttribute:

   [CommandKey(ConsoleKey.Enter)]
   public void DoMoveToNextField()
      => FocusOnNextField();

Capturing events

Maps have their own lifecycle and specific maps can capture those event and take action when they are triggered. Each of these events is associated with a method that can be overridden by map subclasses allowing maps to take specific actions. You might notice these methods to be similar to events on windows when working with desktop applications but implemented using overloading instead of event delegates for the sake of simplicity.

  • OnActivating() is called when the maps is about to become the active map, not only being shown for the first time but also returning to it after the user dismisses another map shown by it;
  • OnDeactivating() is triggered when the map is about to become inactive, either because another is being shown on top of it or it is being closed;
  • OnClosing() is called when the Close() method of the map has been called and it is about to be closed, the method must respond with true or false whether the map can be closed in its current state;
  • OnClosed() happens after the map is effectively closed and is no longer being on the screen;
  • OnKeyPressed() is triggered when any key is pressed by the user and provides an alternative means to handle key pressing by a map (although I particularly believe the CommandKeyAttribute provides a better handling);
  • OnLostFocus allows a map to take action whenever an input field loses focus to another field (or to no field at all).