Add hoverSentence.hs

This commit is contained in:
千住柱間 2025-03-04 03:56:11 +00:00
commit a083733842

62
hoverSentence.hs Normal file
View file

@ -0,0 +1,62 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE OverloadedLabels #-}
{-
this receives a list of strings, thus it's fully compatible with xcdatHS.
when you hover the mouse over a word, it shows the word in stdout.
-}
module Main where
import qualified GI.Gtk as Gtk
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
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)
motionController <- Gtk.eventControllerMotionNew
Gtk.widgetAddController label motionController
void $ Gtk.on motionController #enter $ \_ _ -> putStrLn (T.unpack word)
Gtk.toWidget label
main :: IO ()
main = do
app <- Gtk.applicationNew (Just "com.example.TokenClicker") []
void $ Gio.onApplicationActivate app $ do
window <- Gtk.applicationWindowNew app
Gtk.windowSetTitle window (Just "Token Clicker")
Gtk.windowSetDefaultSize window 400 100
box <- Gtk.boxNew Gtk.OrientationHorizontal 0 -- Set spacing to 0
let tokens = ["これ", "", "日本語", "", "文章", "です"]
-- Create labels for each token
widgets <- mapM (uncurry createWordLabel) (zip [0..] tokens)
mapM_ (Gtk.boxAppend box) widgets
Gtk.windowSetChild window (Just box)
void $ Gtk.on window #closeRequest $ do
Gio.applicationQuit app
return False
Gtk.widgetSetVisible window True
_ <- Gio.applicationRun app Nothing
return ()