disable keyboard shortcuts when text editing

This commit is contained in:
sheaf 2024-11-03 11:46:34 +01:00
parent 197adec8d0
commit 237fb4a7d7
3 changed files with 49 additions and 21 deletions

View file

@ -91,11 +91,14 @@ infixl 1 >>?=
-- controllers attached to it, as we don't want the label to participate in -- controllers attached to it, as we don't want the label to participate in
-- drag-and-drop operations, especially because having it participate in -- drag-and-drop operations, especially because having it participate in
-- drag-and-drop operations triggers segfaults due to a GTK bug. -- drag-and-drop operations triggers segfaults due to a GTK bug.
editableLabelNew :: IO GTK.EditableLabel editableLabelNew :: Text -> IO GTK.EditableLabel
editableLabelNew = do editableLabelNew txt = do
label <- GTK.editableLabelNew " " label <- GTK.editableLabelNew txt
widget <- GTK.toWidget label widget <- GTK.toWidget label
removeControllers widget removeControllers widget
-- Make the label not editable to start with,
-- because we want it to be editable on double-clicks not single clicks.
GTK.editableSetEditable label False
return label return label
where where

View file

@ -13,7 +13,7 @@ module MetaBrush.UI.FileBar
import Control.Monad import Control.Monad
( join, void, when ) ( join, void, when )
import Data.Foldable import Data.Foldable
( sequenceA_ ) ( sequenceA_, traverse_ )
import Data.Traversable import Data.Traversable
( for ) ( for )
@ -77,7 +77,7 @@ import MetaBrush.UI.Viewport
import MetaBrush.Unique import MetaBrush.Unique
( Unique, freshUnique, uniqueText ) ( Unique, freshUnique, uniqueText )
import MetaBrush.GTK.Util import MetaBrush.GTK.Util
( widgetAddClass, widgetAddClasses ) ( editableLabelNew, widgetAddClass, widgetAddClasses )
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -103,8 +103,7 @@ newFileTab
newDocUniq <- STM.atomically $ runReaderT freshUnique uniqueSupply newDocUniq <- STM.atomically $ runReaderT freshUnique uniqueSupply
pure ( newDocUniq, newHistory $ emptyDocument ( "Untitled " <> uniqueText newDocUniq ) ) pure ( newDocUniq, newHistory $ emptyDocument ( "Untitled " <> uniqueText newDocUniq ) )
pgLabel <- GTK.editableLabelNew ( documentName $ documentMetadata $ present thisTabDocHist ) pgLabel <- editableLabelNew ( documentName $ documentMetadata $ present thisTabDocHist )
GTK.editableSetEditable pgLabel False
-- Connect a signal for editing the document name. -- Connect a signal for editing the document name.
-- --
@ -114,6 +113,9 @@ newFileTab
hasFocus <- GTK.widgetHasFocus pgLabel hasFocus <- GTK.widgetHasFocus pgLabel
when hasFocus do when hasFocus do
newDocName <- GTK.editableGetText pgLabel newDocName <- GTK.editableGetText pgLabel
traverse_ ( `GIO.setSimpleActionEnabled` True ) menuActions
-- NB: 'updateUIAction' below will properly re-disable 'undo'
-- and 'redo' if there is nothing to undo or redo.
uiUpdate <- uiUpdate <-
STM.atomically $ do STM.atomically $ do
STM.modifyTVar' openDocumentsTVar STM.modifyTVar' openDocumentsTVar
@ -180,6 +182,8 @@ newFileTab
1 -> do 1 -> do
handleAction uiElts vars ( SwitchTo thisTabDocUnique ) handleAction uiElts vars ( SwitchTo thisTabDocUnique )
when ( nbClicks > 1 ) do when ( nbClicks > 1 ) do
-- Disable all accelerators when doing text entry.
traverse_ ( `GIO.setSimpleActionEnabled` False ) menuActions
GTK.editableSetEditable pgLabel True GTK.editableSetEditable pgLabel True
GTK.editableLabelStartEditing pgLabel GTK.editableLabelStartEditing pgLabel
void $ GTK.widgetGrabFocus pgLabel void $ GTK.widgetGrabFocus pgLabel

View file

@ -22,9 +22,9 @@ module MetaBrush.UI.StrokeTreeView
import Control.Arrow import Control.Arrow
( second ) ( second )
import Control.Monad import Control.Monad
( void ) ( void, when )
import Data.Foldable import Data.Foldable
( for_ ) ( for_, traverse_ )
import Data.List import Data.List
( elemIndex ) ( elemIndex )
import qualified Data.List.NonEmpty as NE import qualified Data.List.NonEmpty as NE
@ -347,7 +347,7 @@ newLayerViewWidget = do
contentBox <- GTK.boxNew GTK.OrientationHorizontal 0 contentBox <- GTK.boxNew GTK.OrientationHorizontal 0
GTK.treeExpanderSetChild expander ( Just contentBox ) GTK.treeExpanderSetChild expander ( Just contentBox )
itemLabel <- editableLabelNew itemLabel <- editableLabelNew " "
GTK.boxAppend contentBox itemLabel GTK.boxAppend contentBox itemLabel
visibleButton <- GTK.buttonNew visibleButton <- GTK.buttonNew
@ -537,22 +537,42 @@ newLayerView uiElts@( UIElements { window, colours } ) vars = mdo
-- EditableLabel -- -- EditableLabel --
------------------- -------------------
labelClicks <- GTK.gestureClickNew
-- Connect a signal for editing the layer name. -- Connect a signal for editing the layer name.
-- --
-- NB: we don't use the 'onEditableChanged' signal, as that updates -- NB: we don't use the 'onEditableChanged' signal, as that updates
-- after every key stroke. -- after every key stroke.
void $ GI.after label ( GI.PropertyNotify #hasFocus ) $ \ _ -> do void $ GI.after label ( GI.PropertyNotify #hasFocus ) $ \ _ -> do
newText <- GTK.editableGetText label hasFocus <- GTK.widgetHasFocus label
layer <- getLayerData listItem when hasFocus do
modifyingCurrentDocument uiElts vars \ doc -> do newText <- GTK.editableGetText label
let doc' = layer <- getLayerData listItem
over ( field' @"documentMetadata" . field' @"layerMetadata" GTK.editableSetEditable label False
. field' @"layerNames" traverse_ ( `GIO.setSimpleActionEnabled` True ) ( menuActions uiElts )
) -- NB: 'modifyingCurrentDocument' below will properly re-disable
( Map.insert ( layerUnique layer ) newText ) -- 'undo' and 'redo' if there is nothing to undo or redo.
doc modifyingCurrentDocument uiElts vars \ doc -> do
return $ let doc' =
UpdateDoc $ UpdateDocumentTo doc' TrivialDiff over ( field' @"documentMetadata" . field' @"layerMetadata"
. field' @"layerNames"
)
( Map.insert ( layerUnique layer ) newText )
doc
return $
UpdateDoc ( UpdateDocumentTo doc' TrivialDiff )
void $ GTK.onGestureClickReleased labelClicks $ \ nbClicks _x _y -> do
button <- GTK.gestureSingleGetCurrentButton ?self
case button of
1 ->
when ( nbClicks > 1 ) do
-- Disable all accelerators when doing text entry.
traverse_ ( `GIO.setSimpleActionEnabled` False ) ( menuActions uiElts )
GTK.editableSetEditable label True
GTK.editableLabelStartEditing label
void $ GTK.widgetGrabFocus label
_ -> return ()
---------------- ----------------
-- DragSource -- -- DragSource --
@ -823,6 +843,7 @@ newLayerView uiElts@( UIElements { window, colours } ) vars = mdo
void $ GTK.onDropTargetLeave dropTarget $ do void $ GTK.onDropTargetLeave dropTarget $ do
dropTargetCleanup dropTargetCleanup
GTK.widgetAddController label labelClicks
GTK.widgetAddController expander dragSource GTK.widgetAddController expander dragSource
GTK.widgetAddController expander dropTarget GTK.widgetAddController expander dropTarget
GTK.widgetAddController expander click GTK.widgetAddController expander click