From 1a0cfa4b41817b2f558bb78ced4ce0520bc907d8 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Mon, 8 Jan 2024 00:34:17 +0300 Subject: [PATCH] Feat(wx): Catch `Escape` in the grid and close the window [skip ci] --- m_librarian/wx/Grids.py | 24 +++++++++++++++++++++++- m_librarian/wx/ListAuthors.py | 7 +++---- m_librarian/wx/ListBooks.py | 8 ++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/m_librarian/wx/Grids.py b/m_librarian/wx/Grids.py index 415f684..fe2f8d0 100644 --- a/m_librarian/wx/Grids.py +++ b/m_librarian/wx/Grids.py @@ -14,11 +14,33 @@ class GridPanel(wx.Panel): self.grid = grid = wx.grid.Grid(self) vsizer.Add(grid, 0, wx.EXPAND, 0) + grid.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK, self.OnDClick) + grid.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) + self.InitGrid() + grid.SetFocus() + + parent.Bind(wx.EVT_ACTIVATE, self.OnActivate) + parent.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus) + self.Bind(wx.EVT_ACTIVATE, self.OnActivate) + self.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus) def InitGrid(self): raise NotImplementedError + def OnDClick(self, event): + raise NotImplementedError + + def OnKeyDown(self, event): + raise NotImplementedError + + def OnActivate(self, event): + if event.GetActive(): + self.grid.SetFocus() + + def OnSetFocus(self, event): + self.grid.SetFocus() + class GridWindow(AWindow): @@ -33,4 +55,4 @@ class GridWindow(AWindow): def OnInit(self): AWindow.OnInit(self) - self.GridPanelClass(self, self.param) + self.panel = self.GridPanelClass(self, self.param) diff --git a/m_librarian/wx/ListAuthors.py b/m_librarian/wx/ListAuthors.py index 3d73a28..8f18a7a 100644 --- a/m_librarian/wx/ListAuthors.py +++ b/m_librarian/wx/ListAuthors.py @@ -36,9 +36,6 @@ class ListAuthorsPanel(GridPanel): grid.AutoSizeColumns() grid.AutoSizeRows() - grid.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK, self.OnDClick) - grid.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) - def listBooks(self, row): authors = self.param['authors'] author = authors[row] @@ -50,7 +47,9 @@ class ListAuthorsPanel(GridPanel): self.listBooks(row) def OnKeyDown(self, event): - if event.GetKeyCode() in (wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER): + if event.GetKeyCode() == wx.WXK_ESCAPE: + self.Parent.Close() + elif event.GetKeyCode() in (wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER): row = self.grid.GetGridCursorRow() self.listBooks(row) else: diff --git a/m_librarian/wx/ListBooks.py b/m_librarian/wx/ListBooks.py index 3bcd331..b1f84a1 100644 --- a/m_librarian/wx/ListBooks.py +++ b/m_librarian/wx/ListBooks.py @@ -52,6 +52,14 @@ class ListBooksPanel(GridPanel): grid.AutoSizeColumns() grid.AutoSizeRows() + def OnDClick(self, event): + pass + + def OnKeyDown(self, event): + if event.GetKeyCode() == wx.WXK_ESCAPE: + self.Parent.Close() + else: + event.Skip() class ListBooksWindow(GridWindow): -- 2.39.5