gi-gtk-declarative
gi-gtk-declarative copied to clipboard
An app with multiple windows
Does not seem possible right now (or am I wrong?). I wish a container for multiple windows was provided.
You're right, it's not explicitly supported right now. You can hack it in by including Window
bins in an existing container, but the problem is that the container grows:
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}
module Windows where
import Control.Monad (void)
import qualified Data.Text as Text
import Data.Vector (Vector)
import qualified Data.Vector as Vector
import GI.Gtk (Box (..), Label (..),
Orientation (..), Window (..))
import GI.Gtk.Declarative
import GI.Gtk.Declarative.App.Simple
type State = ()
data Event = Closed
view' :: State -> AppView Window Event
view' _ =
bin Window [ #title := "Top Window"
, #widthRequest := 600
, on #deleteEvent (const (True, Closed))
]
(container Box [#orientation := OrientationVertical] windows)
where
windows :: Vector (BoxChild Event)
windows = Vector.generate 5 $ \(succ -> i) ->
let
lbl = "Window " <> Text.pack (show i)
size = 600 - fromIntegral i * 25
in
BoxChild defaultBoxChildProperties $
bin Window [ #title := lbl
, #widthRequest := size
, #heightRequest := size
, on #deleteEvent (const (True, Closed))
]
(widget Label [#label :=lbl])
update' :: State -> Event -> Transition State Event
update' _ Closed = Exit
main :: IO ()
main = void $ run App
{ view = view'
, update = update'
, inputs = []
, initialState = ()
}
As you can see, "Top Window" seems to take the full height of the child windows:
I can see two solutions:
- Create a specific window container that is only used for spawning sub-windows, that will not add them as widgets in the container taking any space
- Have some sort of edge case check in regular widgets (if the child widget to add is a
Window
or subtype) and if so only instantiate it, don't add it to the container
I'm not sure which approach is better, but I'm leaning towards 1. Would you be interested in providing a PR?
Would you be interested in providing a PR?
I'll give it a shot