fix out of range hovering

This commit is contained in:
千住柱間 2025-03-04 04:04:53 +00:00
commit 7b84128d9c

View file

@ -14,43 +14,43 @@ import qualified GI.Gio as Gio
import qualified Data.Text as T
import Control.Monad (void)
createWordLabel :: Int -> T.Text -> IO Gtk.Widget
createWordLabel idx word = do
createWordLabel :: T.Text -> IO Gtk.Widget
createWordLabel word = do
label <- Gtk.labelNew (Just word)
-- Gesture for click events
gesture <- Gtk.gestureClickNew
Gtk.widgetAddController label gesture
void $ Gtk.onGestureClickPressed gesture $ \_ _ _ -> do
putStrLn $ "Clicked token " ++ show idx ++ ": " ++ T.unpack word
-- Motion controller for hover detection (prints hovered word)
-- Gesture for hover detection
motionController <- Gtk.eventControllerMotionNew
Gtk.widgetAddController label motionController
-- Print only when hovering an actual word
void $ Gtk.on motionController #enter $ \_ _ -> putStrLn (T.unpack word)
Gtk.toWidget label
main :: IO ()
main = do
app <- Gtk.applicationNew (Just "com.example.TokenClicker") []
app <- Gtk.applicationNew (Just "com.example.YomichanHover") []
void $ Gio.onApplicationActivate app $ do
window <- Gtk.applicationWindowNew app
Gtk.windowSetTitle window (Just "Token Clicker")
Gtk.windowSetTitle window (Just "Yomichan Hover")
Gtk.windowSetDefaultSize window 400 100
box <- Gtk.boxNew Gtk.OrientationHorizontal 0 -- Set spacing to 0
-- Outer box (centers content)
outerBox <- Gtk.boxNew Gtk.OrientationVertical 0
Gtk.widgetSetHalign outerBox Gtk.AlignCenter
Gtk.widgetSetValign outerBox Gtk.AlignCenter
-- Sentence box (wraps words tightly)
sentenceBox <- Gtk.boxNew Gtk.OrientationHorizontal 0 -- 0 spacing = no visual spaces
Gtk.widgetSetHalign sentenceBox Gtk.AlignCenter
let tokens = ["これ", "", "日本語", "", "文章", "です"]
widgets <- mapM createWordLabel tokens
mapM_ (Gtk.boxAppend sentenceBox) widgets
-- Create labels for each token
widgets <- mapM (uncurry createWordLabel) (zip [0..] tokens)
mapM_ (Gtk.boxAppend box) widgets
Gtk.windowSetChild window (Just box)
Gtk.boxAppend outerBox sentenceBox
Gtk.windowSetChild window (Just outerBox)
void $ Gtk.on window #closeRequest $ do
Gio.applicationQuit app