-
-
Save YoEight/11356454 to your computer and use it in GitHub Desktop.
My https://hackage.haskell.org/package/mvc-1.0.0 playground
This file contains hidden or 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
GHC: 7.8.2 | |
Cabal: 1.18.0.3 |
This file contains hidden or 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
import Control.Applicative (liftA2) | |
import Control.Monad (forever) | |
import Control.Monad.State | |
import Control.Monad.Trans | |
import Data.Foldable (traverse_, for_) | |
import Data.Functor (void) | |
import Data.Monoid ((<>)) | |
import qualified Graphics.UI.Gtk as Gtk | |
import MVC | |
import MVC.Prelude | |
import Pipes | |
import Pipes.Concurrent | |
import System.FilePath | |
data Inputs = ISrc FilePath | |
| IDest FilePath | |
| IDoIt | |
data Outputs = OSrc FilePath | |
| ODest FilePath | |
| ODoIt FilePath FilePath | |
data AppState = | |
AppState | |
{ appSrc :: Maybe FilePath | |
, appDest :: Maybe FilePath | |
} | |
initState :: AppState | |
initState = AppState Nothing Nothing | |
main :: IO () | |
main = do | |
Gtk.initGUI | |
win <- Gtk.windowNew | |
-- Open File Dialog | |
odialog <- Gtk.fileChooserDialogNew (Just "Choose Source") | |
(Just win) | |
Gtk.FileChooserActionOpen | |
[("Open", Gtk.ResponseOk), ("Cancel", Gtk.ResponseCancel)] | |
-- Save File Dialog | |
sdialog <- Gtk.fileChooserDialogNew (Just "Choose Destination") | |
(Just win) | |
Gtk.FileChooserActionSave | |
[("Save", Gtk.ResponseOk), ("Cancel", Gtk.ResponseCancel)] | |
-- Vbox | |
vbox <- Gtk.vBoxNew True 10 | |
srcb <- Gtk.buttonNewWithLabel "Choose source..." | |
destb <- Gtk.buttonNewWithLabel "Choose destination..." | |
dob <- Gtk.buttonNewWithLabel "Do it !" | |
Gtk.widgetSetSensitive destb False | |
Gtk.widgetSetSensitive dob False | |
Gtk.containerAdd vbox srcb | |
Gtk.containerAdd vbox destb | |
Gtk.containerAdd vbox dob | |
(output, input) <- spawn Unbounded | |
-- Src Button | |
Gtk.on srcb Gtk.buttonActivated $ do | |
opt <- getSelection odialog | |
traverse_ (forkIO . void . atomically . send output . ISrc) opt | |
-- Dest Button | |
Gtk.on destb Gtk.buttonActivated $ do | |
opt <- getSelection sdialog | |
traverse_ (forkIO . void . atomically . send output . IDest) opt | |
-- Do it Button | |
Gtk.on dob Gtk.buttonActivated $ | |
void $ forkIO $ void $ atomically $ send output IDoIt | |
let controller = asInput input | |
view = asSink $ \i -> Gtk.postGUISync $ | |
case i of | |
OSrc path -> do | |
Gtk.buttonSetLabel srcb (takeFileName path) | |
Gtk.widgetSetSensitive destb True | |
ODest path -> do | |
Gtk.buttonSetLabel destb (takeFileName path) | |
Gtk.widgetSetSensitive dob True | |
ODoIt from to -> readFile from >>= writeFile to | |
-- Model | |
model = asPipe $ forever $ do | |
i <- await | |
s <- get | |
case i of | |
ISrc path -> do | |
put s{ appSrc = Just path } | |
yield $ OSrc path | |
IDest path -> do | |
put s{ appDest = Just path } | |
yield $ ODest path | |
IDoIt -> | |
let zipped = liftA2 (,) (appSrc s) (appDest s) in | |
traverse_ (yield . uncurry ODoIt) zipped | |
-- Configure main window | |
Gtk.set win [ Gtk.windowTitle Gtk.:= "MVC Test" | |
, Gtk.windowDefaultWidth Gtk.:= 200 | |
, Gtk.containerBorderWidth Gtk.:= 10 | |
, Gtk.containerChild Gtk.:= vbox | |
] | |
Gtk.on win Gtk.objectDestroy Gtk.mainQuit | |
Gtk.widgetShowAll win | |
-- Run our MVC | |
forkIO $ void $ runMVC initState model $ return (view, controller) | |
Gtk.mainGUI | |
getSelection :: Gtk.FileChooserDialog -> IO (Maybe FilePath) | |
getSelection diag = do | |
resp <- Gtk.dialogRun diag | |
Gtk.widgetHide diag | |
case resp of | |
Gtk.ResponseOk -> Gtk.fileChooserGetFilename diag | |
_ -> return Nothing |
This file contains hidden or 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
name: test-mvc | |
version: 0.1.0.0 | |
build-type: Simple | |
cabal-version: >=1.10 | |
executable test-mvc | |
main-is: Main.hs | |
build-depends: base >=4.7 && <4.8, | |
mvc, | |
gtk3, | |
mtl, | |
pipes, | |
pipes-concurrency, | |
filepath | |
default-language: Haskell2010 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
This is a simple file copier. It got 3 buttons:
Choose source
: Select a file to copy fromChoose destination
: Select a file to write toDo it
: Performs copyImplementation
ISrc
when an source file is selected. The model yieldsOSrc
when it getsISrc
IDest
when an destination file is selected. The model yieldsODest
when it getsIDest
IDoIt
whenDo It
button is clicked. The model yieldsODoIt
when it getsIDoIt
Issues
I have to click several time on a button to get it's view action executed. I don't know if that behaviour is related to how I configured the mailbox or another misuse of the library.
Some figures.
To get
OSrc
view action executed, I have to select a file ~4 timesTo get
ODest
view action executed, I have to select a file 4-8 timesTo get
ODoIt
view action executed, I have to click onDo it
~10 times