Skip to content

Commit

Permalink
added mithril example
Browse files Browse the repository at this point in the history
  • Loading branch information
hamaluik committed Jul 7, 2020
1 parent 516fca0 commit f54a285
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/file-dialogs/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use cef_simple::{Cef, WindowOptions};
use simplelog::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let cef = Cef::initialize(None, true)?;
let cef = Cef::initialize(Some(8000), false)?;

CombinedLogger::init(vec![TermLogger::new(
LevelFilter::Trace,
Expand Down
14 changes: 13 additions & 1 deletion examples/file-dialogs/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CEF File Dialogs Demo</title>
<script>
function createFrame() {
var el = document.createElement("iframe");
el.src = "https://hamaluik.ca/";
el.id = "theframe";
document.body.appendChild(el);
}
function closeFrame() {
var frame = document.getElementById("theframe");
if(!frame) return;
document.body.removeChild(frame);
}
function handleOpen() {
cef.openFileDialog("Open", null, "Text Files|.txt")
.then((path) => {
Expand All @@ -14,7 +25,7 @@
.catch((error) => console.error("failed to run save dialog:", error));
}
function handleSave() {
cef.openFileDialog("Save", null, "Text Files|.txt")
cef.saveFileDialog("Save", null, "Text Files|.txt")
.then((path) => {
console.info("chosen path:");
console.info(path);
Expand All @@ -24,6 +35,7 @@
</script>
</head>
<body>
<button onclick="createFrame(); return false;">Frame</button> <button onclick="closeFrame(); return false;">Close</button>
<button onclick="handleOpen(); return false;">Open</button>&nbsp;<button onclick="handleSave(); return false;">Save</button>
</body>
</html>
90 changes: 90 additions & 0 deletions examples/mithril/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CEF Mithril Demo</title>
<style>
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
position: relative;
background: #ccc;
}

.modal {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
align-items: center;
justify-content: center;
}
.background {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.5);
z-index: 0;
}
.content {
background: white;
border-radius: 0.5em;
z-index: 999;
width: 80%;
height: 80%;
margin: 0;
padding: 0;
}
iframe {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<script src="https://unpkg.com/mithril/mithril.js"></script>
<script>
var showFrame = false;
var Frame = {
view: function(vnode) {
if(!showFrame) return null;
return [
m(".modal", [
m(".background", {
onclick: function() { showFrame = false; }
}),
m(".content", [
m("iframe", { src: "https://blog.hamaluik.ca/" })
])
])
];
}
};

var Hello = {
oninit: function(vnode) {
vnode.state.count = 0;
},
view: function(vnode) {
return [
m("h1", "Mithril Demo"),
m("button", { onclick: function() { vnode.state.count++ } }, vnode.state.count + " clicks"),
m("button", { onclick: function() { showFrame = true; } }, "Help"),
m("button", { onclick: function() { cef.saveFileDialog("Save", null, "Text Files|.txt"); } }, "Save"),
m(Frame),
];
}
};
m.mount(document.body, Hello);
</script>
</body>
</html>
33 changes: 33 additions & 0 deletions examples/mithril/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use cef_simple::{Cef, WindowOptions};
use simplelog::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
CombinedLogger::init(vec![TermLogger::new(
LevelFilter::Trace,
Config::default(),
TerminalMode::Mixed,
)])
.unwrap();

let cef = Cef::initialize(Some(8000), false)?;

let page = urlencoding::encode(include_str!("index.html"));

cef.open_window(WindowOptions {
url: format!("data:text/html,{}", page),
title: Some("CEF Mithril Demo".to_string()),
window_icon: Some(include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/icon.png"
))),
window_app_icon: Some(include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/icon.png"
))),
..WindowOptions::default()
})?;

cef.run()?;

Ok(())
}

0 comments on commit f54a285

Please sign in to comment.