Add hoverSentence.hs
This commit is contained in:
parent
edfb08457b
commit
a083733842
1 changed files with 62 additions and 0 deletions
62
hoverSentence.hs
Normal file
62
hoverSentence.hs
Normal 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 ()
|
||||
Loading…
Reference in a new issue