How to display custom Sidebar - Panel?
I followed Create a Panel for an extension I am working on.
My implementation has a Text box and a couple of buttons.
The panel is a Top Window that can be docked on the side.
The the purpose if the Panel is to output Log information from Python run in the Spreadsheet.
Functionally the logging is working. When the window is open Logging output shows up in the window.
The Issue!
When a second document is opened when a panel is docked it also creates a second panel in the new window.
The new panel continues to log from the first window. When I manually hide the new panel and then show it again it works as expected.
This is the trigger responsible for toggling the panel.
def create_service(ctx, name: str, args: Any = None) -> Any:
"""Create service with args if required."""
smgr = ctx.getServiceManager()
if args:
return smgr.createInstanceWithArgumentsAndContext(name, args, ctx)
else:
return smgr.createInstanceWithContext(name, ctx)
class Switcher(XJobExecutor, XServiceInfo, unohelper.Base):
# other code ...
def trigger(self, arg):
global RES_LOG_WIN_URL
desktop = create_service(self.ctx, "com.sun.star.frame.Desktop")
doc = desktop.getCurrentComponent()
layoutmgr = doc.getCurrentController().getFrame().LayoutManager
if layoutmgr.isElementVisible(RES_LOG_WIN_URL):
layoutmgr.hideElement(RES_LOG_WIN_URL)
else:
layoutmgr.requestElement(RES_LOG_WIN_URL)
I thought I could just toggle the window when the new document is loaded seeing as it works after the window is manually toggled via a menu entry. I created a job for this purpose.
This job run OnViewCreated
Event.
class LogWindowJob(XJob, unohelper.Base):
"""Python UNO Component that implements the com.sun.star.task.Job interface."""
# other code ...
def execute(self, args: Any) -> None:
global RES_LOG_WIN_URL
self._logger.debug("execute")
try:
desktop = create_service(self.ctx, "com.sun.star.frame.Desktop")
doc = desktop.getCurrentComponent()
layout_mgr = new_doc.getCurrentController().getFrame().LayoutManager
if layout_mgr.isElementVisible(RES_LOG_WIN_URL):
layout_mgr.hideElement(RES_LOG_WIN_URL)
self._logger.debug("Log Window Job Done")
except Exception as e:
self._logger.error("Error Setting Custom Properties", exc_info=True)
For some reason that I can’t figure out, when the job is executed it actually hides the panel in the first window and not the new window.
How do I get access to the correct panel when the document loads?
I don’t know if there is a better way to put a Panel into the sidebar. If there is please point me to any resources that you are aware of on this matter.