-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Draft] Multiple top-level windows (and some questions) #75
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
00745ee
Add a destroy method to Patchable, rather than always using Gtk.widge…
Dretch e9b6032
Initial draft of secondary top-level windows.
Dretch 0c4c99b
Fix silly naming.
Dretch dbed817
Fix some warnings.
Dretch 05b2e2d
Call the destroy method on windows in window hosts
Dretch d7851ea
Document the destroy method in Patchable
Dretch 539cb35
Fix more warnings.
Dretch 3e8e72a
Fix module doc-comment
Dretch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE OverloadedLabels #-} | ||
{-# LANGUAGE OverloadedLists #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Windows where | ||
|
||
import Control.Concurrent (threadDelay) | ||
import Control.Monad (void) | ||
import Data.Text (pack) | ||
import Data.Vector (Vector) | ||
import qualified Data.Vector as Vector | ||
import Pipes.Prelude (repeatM) | ||
|
||
import GI.Gtk (Box (..), Button (..), | ||
Label (..), Orientation (..), | ||
Window (..)) | ||
import GI.Gtk.Declarative | ||
import GI.Gtk.Declarative.App.Simple | ||
|
||
type State = Vector (Maybe Int) | ||
|
||
data Event | ||
= IncrAll | ||
| AddWindow | ||
| CloseWindow Int | ||
| RemoveWindow | ||
| Closed | ||
|
||
view' :: State -> AppView Window Event | ||
view' ns = | ||
bin | ||
Window | ||
[ #title := "Windows" | ||
, on #deleteEvent (const (True, Closed)) | ||
, #widthRequest := 200 | ||
, #heightRequest := 300 | ||
] | ||
$ container Box [#orientation := OrientationVertical, #spacing := 4, #margin := 4] | ||
$ [addButton, removeButton ns] | ||
<> Vector.imap windowLabel ns | ||
|
||
addButton :: BoxChild Event | ||
addButton = BoxChild defaultBoxChildProperties | ||
$ widget Button [#label := "Add Window", on #clicked AddWindow] | ||
|
||
removeButton :: State -> BoxChild Event | ||
removeButton ns = BoxChild defaultBoxChildProperties $ widget | ||
Button | ||
[ #label := "Remove Window" | ||
, #sensitive := (ns /= mempty) | ||
, on #clicked RemoveWindow | ||
] | ||
|
||
windowLabel :: Int -> Maybe Int -> BoxChild Event | ||
windowLabel i n = | ||
BoxChild defaultBoxChildProperties { padding = 4 } | ||
$ windowHost (window i <$> n) | ||
$ windowChild i n | ||
|
||
window :: Int -> Int -> Bin Window Event | ||
window i x = bin | ||
Window | ||
[ #title := pack ("Window " <> show i) | ||
, on #deleteEvent (const (True, CloseWindow i)) | ||
] | ||
(widget Label [#label := pack ("Open for " <> show x <> " seconds")]) | ||
|
||
windowChild :: Int -> Maybe Int -> Widget Event | ||
windowChild i = \case | ||
Nothing -> widget Label [#label := pack ("Window " <> show i <> " Closed")] | ||
Just _ -> widget Label [#label := pack ("Window " <> show i <> " Open")] | ||
|
||
update' :: State -> Event -> Transition State Event | ||
update' ns = \case | ||
IncrAll -> Transition (fmap succ <$> ns) (return Nothing) | ||
AddWindow -> Transition (ns `Vector.snoc` Just 1) (return Nothing) | ||
CloseWindow i -> Transition | ||
(Vector.imap (\i' n -> if i' == i then Nothing else n) ns) | ||
(return Nothing) | ||
RemoveWindow -> Transition (Vector.init ns) (return Nothing) | ||
Closed -> Exit | ||
|
||
main :: IO () | ||
main = void $ run App | ||
{ view = view' | ||
, update = update' | ||
, inputs = [incrPeriodically] | ||
, initialState = [] | ||
} | ||
where incrPeriodically = repeatM $ IncrAll <$ threadDelay (1000 * 1000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to call
destroy
here witholdChildState
, but we can't do that becausedestroy
also requires the old declarative widget - which we don't have.Does this situation ever actually happen? It seems to me that if
oldChildState
exists then we should also have the old declarative widget (since that would have been used to createoldChildState
).