fjvallarino/monomer

File system access

Closed this issue · 6 comments

Not sure if this is the best way to ask noob questions ....

Does monomer give file system access?

((Borrowed an intel mac so as to build monomer -- Yay!!))

Hi @jxxcarlson!

When handling an event, one of the values you can return is a Task. This value requires you to pass an IO action, where you can execute arbitrary code.

For example, modifying the Tutorial 01 a bit:

newtype AppModel = AppModel {
  _clickCount :: Int
} deriving (Eq, Show)

data AppEvent
  = AppInit
  | AppIgnore
  | AppIncrease
  | AppLoadPrint
  deriving (Eq, Show)

makeLenses 'AppModel

buildUI
  :: WidgetEnv AppModel AppEvent
  -> AppModel
  -> WidgetNode AppModel AppEvent
buildUI wenv model = widgetTree where
  widgetTree = vstack [
      label "Hello world",
      spacer,
      hstack [
        label $ "Click count: " <> showt (model ^. clickCount),
        spacer,
        button "Increase count" AppIncrease,
        spacer,
        button "Increase count" AppLoadPrint
      ]
    ] `styleBasic` [padding 10]

handleEvent
  :: WidgetEnv AppModel AppEvent
  -> WidgetNode AppModel AppEvent
  -> AppModel
  -> AppEvent
  -> [AppEventResponse AppModel AppEvent]
handleEvent wenv node model evt = case evt of
  AppInit -> []
  AppIncrease -> [Model (model & clickCount +~ 1)]
  AppLoadPrint -> [Task $ do
    -- Here you can do anything allow in IO; for example, read files
    print "Test"
    -- A Task requires returning an event, since in general you want to notify users about the result of the action
    return AppIgnore
  ]

Tutorial 04 explains how Tasks work, and can give you a better idea of what you can do. Looking at the Books example may also help.

Hi @fjvallarino, Your comments and the Book example were of enormous help — thank you very much!

I've made quite a bit of progress (see screenshot below) — can open a file, with the contents displayed in the left text area and the compiled-to-HTML version displayed as text in the right window. I am using Blaze for the Html.

I would now like for the HTML version to be rendered as HTML in the right window. How might I go about doing that?

I can already tell that Monomer is awesome!!

Screen Shot 2022-09-15 at 1 08 39 AM

PS. (1) The markup language (L0) is an experimental one. Here is an example of a real document written in L0: https://scripta.io/s/jxxcarlson:yoneda-lemma . The Elm version of the Scripta compiler can also handle versions of LaTeX and Markdown. The Haskell version will eventually handle these as well.

(2) The source text is at https://github.com/jxxcarlson/scripta-monomer

Since Monomer uses OpenGL for rendering, and its content does not use the OS' regular UI, integrating the browser into an application is not possible. I remember seeing some years ago libraries for rendering HTML content into a texture, but even if it works well I think it would still be too painful.

Alternatively, you can try to parse you original format and render it with custom code (I think parsing that format will be simpler than parsing HTML).

Considering the need for a browser engine, maybe Obelisk is a good fit for this project?

Thanks! Will investigate Obelisk. One potentially big obstacle is that I rely on KaTeX, a javascript library to render math equations.

Yes, in that case using a browser based solution is really the only option.

I'll close the issue for the time being. If you have any other questions, please re-open it or create a new one. Thanks!

Thanks so much!! I love monomer.