Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a2b11fc82b | |||
| 72fdc2fffe | |||
| 309edca4f4 | |||
| cd8a22182d |
1 changed files with 93 additions and 0 deletions
93
dynamicUI.hs
Normal file
93
dynamicUI.hs
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE OverloadedLabels #-}
|
||||
|
||||
-- Copyright © 2025 Hashirama Senju
|
||||
|
||||
{-
|
||||
we dynamically update the UI when new data comes to the entry
|
||||
-}
|
||||
|
||||
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 :: T.Text -> IO Gtk.Widget
|
||||
createWordLabel word = do
|
||||
label <- Gtk.labelNew (Just 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
|
||||
|
||||
updateUI :: Gtk.Box -> [String] -> IO ()
|
||||
updateUI outerBox sentence = do
|
||||
-- Remove all existing children from outerBox
|
||||
let removeChildren widget = do
|
||||
mChild <- Gtk.widgetGetFirstChild widget
|
||||
case mChild of
|
||||
Just child -> do
|
||||
Gtk.boxRemove outerBox child
|
||||
removeChildren widget
|
||||
Nothing -> return ()
|
||||
removeChildren outerBox
|
||||
|
||||
-- Create a new sentence box
|
||||
sentenceBox <- Gtk.boxNew Gtk.OrientationHorizontal 0
|
||||
Gtk.widgetSetHalign sentenceBox Gtk.AlignCenter
|
||||
|
||||
let tokens = map T.pack sentence
|
||||
widgets <- mapM createWordLabel tokens
|
||||
mapM_ (Gtk.boxAppend sentenceBox) widgets
|
||||
|
||||
Gtk.boxAppend outerBox sentenceBox
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
app <- Gtk.applicationNew (Just "com.example.YomichanHover") []
|
||||
|
||||
void $ Gio.onApplicationActivate app $ do
|
||||
window <- Gtk.applicationWindowNew app
|
||||
Gtk.windowSetTitle window (Just "Yomichan Hover")
|
||||
Gtk.windowSetDefaultSize window 400 150
|
||||
|
||||
finalBox <- Gtk.boxNew Gtk.OrientationVertical 10
|
||||
Gtk.widgetSetHalign finalBox Gtk.AlignCenter
|
||||
Gtk.widgetSetValign finalBox Gtk.AlignCenter
|
||||
Gtk.windowSetChild window (Just finalBox)
|
||||
|
||||
-- Input field
|
||||
entry <- Gtk.entryNew
|
||||
Gtk.boxAppend finalBox entry
|
||||
Gtk.editableSetEditable entry True
|
||||
|
||||
-- Outer box (centers content)
|
||||
outerBox <- Gtk.boxNew Gtk.OrientationVertical 0
|
||||
Gtk.widgetSetHalign outerBox Gtk.AlignCenter
|
||||
Gtk.widgetSetValign outerBox Gtk.AlignCenter
|
||||
Gtk.boxAppend finalBox outerBox
|
||||
|
||||
|
||||
-- Connect signal to update UI when pressing Enter
|
||||
void $ Gtk.on entry #activate $ do
|
||||
text <- Gtk.editableGetText entry
|
||||
let sentence = words (T.unpack text)
|
||||
updateUI outerBox sentence
|
||||
|
||||
|
||||
|
||||
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