Created
December 3, 2019 10:01
-
-
Save rondreas/ad0be0052c48d83bdebeb739435ea11a to your computer and use it in GitHub Desktop.
Example of a Form Command List with a Notifier and Command to call for the list to update.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Example of how to dynamically change a Form List Command. | |
We will implement a button that emits a notifier, which our form list | |
command will listen for when notified, it will refresh. | |
""" | |
import random | |
import lx | |
import lxu | |
import lxifc | |
def get_commands(): | |
""" Get a randomized list of available commands, | |
:returns: List of string for command names | |
:rtype: str list | |
""" | |
cmd_svc = lx.service.Command() | |
all_commands = [] | |
for index in range(cmd_svc.CommandCount()): | |
command = cmd_svc.CommandByIndex(index) | |
all_commands.append(command.Name()) | |
return random.sample(all_commands, random.randrange(3, 15)) | |
class Notifier(lxifc.Notifier): | |
masterlist = {} | |
def noti_Name(self): | |
return "my.notifier" | |
def noti_AddClient(self, event): | |
self.masterlist[event.__peekobj__()] = event | |
def noti_RemoveClient(self, event): | |
del self.masterlist[event.__peekobj__()] | |
def Notify(self, flags): | |
for event in self.masterlist: | |
e = lx.object.CommandEvent(self.masterlist[event]) | |
e.Event(flags) | |
class UpdateCommand(lxu.command.BasicCommand): | |
def __init__(self): | |
lxu.command.BasicCommand.__init__(self) | |
def basic_ButtonName(self): | |
return "Update" | |
def basic_Execute(self, msg, flags): | |
""" This command will "send" the notifier when executed, | |
Available flags to pass the `Notify`: | |
fCMDNOTIFY_CHANGE_ALL = 15 | |
fCMDNOTIFY_DATATYPE = 8 | |
fCMDNOTIFY_DISABLE = 4 | |
fCMDNOTIFY_LABEL = 2 | |
fCMDNOTIFY_VALUE = 1 | |
""" | |
notifier = Notifier() | |
notifier.Notify(lx.symbol.fCMDNOTIFY_DATATYPE) | |
class CommandList(lxifc.UIValueHints): | |
""" The UI Value Hints that we will populate with a list of commands. """ | |
def __init__(self, items): | |
self._items = items | |
def uiv_Flags(self): | |
return lx.symbol.fVALHINT_FORM_COMMAND_LIST | |
def uiv_FormCommandListCount(self): | |
return len(self._items) | |
def uiv_FormCommandListByIndex(self,index): | |
return self._items[index] | |
class FormCommandList(lxu.command.BasicCommand): | |
""" A Form Command List is not really a command. So we will not implement an | |
execute or query method. The important part is the UI Value Hints, which will | |
tell any form that uses this 'command' what to look like. | |
In our case we will get a set of random commands that will populate in the | |
place for this command. | |
""" | |
def __init__(self): | |
lxu.command.BasicCommand.__init__(self) | |
self.dyna_Add('cmds', lx.symbol.sTYPE_INTEGER) # Add an integer attribute. The attribute is required | |
self.basic_SetFlags(0, lx.symbol.fCMDARG_QUERY) # mark it as queriable | |
self.not_svc = lx.service.NotifySys() | |
self._notifier = lx.object.Notifier() | |
def arg_UIValueHints(self, index): | |
if index == 0: | |
return CommandList(get_commands()) | |
def cmd_Execute(self,flags): | |
pass # dummy execute method | |
def cmd_Query(self,index,vaQuery): | |
pass # dummy query method | |
def cmd_NotifyAddClient(self, argument, object): | |
self._notifier = self.not_svc.Spawn("my.notifier", "") | |
self._notifier.AddClient(object) | |
def cmd_NotifyRemoveClient(self, object): | |
self._notifier.RemoveClient(object) | |
lx.bless(Notifier, "my.notifier") | |
lx.bless(UpdateCommand, "my.update") | |
lx.bless(FormCommandList, "my.formlistcommand") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<configuration> | |
<atom type="Attributes"> | |
<hash type="Sheet" key="example_formlistcommand:sheet"> | |
<atom type="Label">Toolbar</atom> | |
<!-- Export flag defines that this can be included in other forms. --> | |
<atom type="Export">1</atom> | |
<atom type="Layout">vtoolbar</atom> | |
<atom type="IconMode">both</atom> | |
<list type="Control" val="cmd my.update"></list> | |
<list type="Control" val="cmd my.formlistcommand ?"></list> | |
</hash> | |
</atom> | |
</configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment