Skip to content

Commit

Permalink
Add AboutSixtyFPS element
Browse files Browse the repository at this point in the history
  • Loading branch information
tronical committed Oct 5, 2021
1 parent d798947 commit 4a26fae
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,7 @@ All notable changes to this project will be documented in this file.
- `sixtyfps::Image` has now a `path()` accessor function in Rust and C++ to access the optional path
of the file on disk that's backing the image.
- New `moved` and `pointer-event` callback in `TouchArea`
- New `AboutSixtyFPS` widget

### Fixed

Expand Down
14 changes: 14 additions & 0 deletions docs/widgets.md
Expand Up @@ -396,3 +396,17 @@ Example := Window {

That's the same as `HorizontalLayout`, `VerticalLayout` or `GridLayout` but the spacing and padding values
depending on the style instead of defaulting to 0.

## `AboutSixtyFPS`

This element displays the a "Made with SixtyFPS" badge.

```60
import { AboutSixtyFPS } from "sixtyfps_widgets.60";
Example := Window {
width: 128px;
height: 128px;
AboutSixtyFPS {
}
}
```
10 changes: 9 additions & 1 deletion examples/gallery/gallery.60
Expand Up @@ -8,7 +8,7 @@
Please contact info@sixtyfps.io for more information.
LICENSE END */
import { ScrollView, Button, CheckBox, SpinBox, Slider, GroupBox, LineEdit, StandardListView,
ComboBox, HorizontalBox, VerticalBox, GridBox, TabWidget, TextEdit } from "sixtyfps_widgets.60";
ComboBox, HorizontalBox, VerticalBox, GridBox, TabWidget, TextEdit, AboutSixtyFPS } from "sixtyfps_widgets.60";

App := Window {
preferred-width: 500px;
Expand Down Expand Up @@ -149,6 +149,14 @@ App := Window {
}
}
}
Tab {
title: "About";
HorizontalBox {
alignment: center;
AboutSixtyFPS {
}
}
}
}
}
}
8 changes: 7 additions & 1 deletion sixtyfps_compiler/build.rs
Expand Up @@ -55,7 +55,13 @@ fn process_style(path: &Path) -> std::io::Result<String> {
.filter_map(Result::ok)
.filter(|entry| {
entry.path().is_file()
&& entry.path().extension().unwrap_or_default() == std::ffi::OsStr::new("60")
&& entry
.path()
.extension()
.map(|ext| {
ext == std::ffi::OsStr::new("60") || ext == std::ffi::OsStr::new("svg")
})
.unwrap_or_default()
})
.map(|entry| entry.path())
.collect();
Expand Down
9 changes: 7 additions & 2 deletions sixtyfps_compiler/generator/cpp.rs
Expand Up @@ -599,8 +599,13 @@ pub fn generate(doc: &Document, diag: &mut BuildDiagnostics) -> Option<impl std:

file.declarations.extend(doc.root_component.embedded_file_resources.borrow().iter().map(
|(path, id)| {
let data =
std::fs::read(path).expect(&format!("unable to read file for embedding: {}", path));
let data = match path.strip_prefix("builtin:/") {
None => std::fs::read(path)
.expect(&format!("unable to read file for embedding: {}", path)),
Some(builtin) => crate::library::load_file(std::path::Path::new(builtin))
.expect("non-existent internal file referenced")
.to_vec(),
};

let mut init = "{ ".to_string();

Expand Down
15 changes: 14 additions & 1 deletion sixtyfps_compiler/generator/rust.rs
Expand Up @@ -656,7 +656,8 @@ fn generate_component(
.iter()
.map(|(path, id)| {
let symbol = format_ident!("SFPS_EMBEDDED_RESOURCE_{}", id);
quote!(const #symbol: &'static [u8] = ::core::include_bytes!(#path);)
let data = embedded_file_tokens(path);
quote!(const #symbol: &'static [u8] = #data;)
})
.collect();

Expand Down Expand Up @@ -2083,3 +2084,15 @@ fn compile_path(path: &Path, component: &Rc<Component>) -> TokenStream {
fn access_component_field_offset(component_id: &Ident, field: &Ident) -> TokenStream {
quote!({ *&#component_id::FIELD_OFFSETS.#field })
}

fn embedded_file_tokens(path: &str) -> TokenStream {
match path.strip_prefix("builtin:/") {
None => quote!(::core::include_bytes!(#path)),
Some(builtin) => {
let data = crate::library::load_file(std::path::Path::new(builtin))
.expect("non-existent internal file referenced");
let literal = proc_macro2::Literal::byte_string(data);
quote!(#literal)
}
}
}
17 changes: 17 additions & 0 deletions sixtyfps_compiler/lib.rs
Expand Up @@ -135,4 +135,21 @@ pub async fn compile_syntax_node(

mod library {
include!(env!("SIXTYFPS_WIDGETS_LIBRARY"));

pub fn load_file(builtin_path: &std::path::Path) -> Option<&'static [u8]> {
let mut components = vec![];
for part in builtin_path.iter() {
if part == ".." {
components.pop();
} else if part != "." {
components.push(part);
}
}
if let &[folder, file] = components.as_slice() {
let library = widget_library().iter().find(|x| x.0 == folder)?.1;
library.iter().find_map(|vf| if vf.path == file { Some(vf.contents) } else { None })
} else {
None
}
}
}
4 changes: 1 addition & 3 deletions sixtyfps_compiler/passes.rs
Expand Up @@ -78,9 +78,7 @@ pub async fn run_passes(
lower_tabwidget::lower_tabwidget(component, &mut type_loader, diag).await;
}

if compiler_config.embed_resources {
embed_resources::embed_resources(root_component);
}
embed_resources::embed_resources(root_component, compiler_config.embed_resources);

inlining::inline(doc);
focus_item::resolve_element_reference_in_set_focus_calls(root_component, diag);
Expand Down
16 changes: 10 additions & 6 deletions sixtyfps_compiler/passes/embed_resources.rs
Expand Up @@ -13,26 +13,28 @@ use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

pub fn embed_resources(component: &Rc<Component>) {
pub fn embed_resources(component: &Rc<Component>, embed_files_by_default: bool) {
let global_embedded_resources = &component.embedded_file_resources;

for component in
component.used_types.borrow().sub_components.iter().chain(std::iter::once(component))
{
visit_all_expressions(component, |e, _| {
embed_resources_from_expression(e, global_embedded_resources)
embed_resources_from_expression(e, global_embedded_resources, embed_files_by_default)
});
}
}

fn embed_resources_from_expression(
e: &mut Expression,
global_embedded_resources: &RefCell<HashMap<String, usize>>,
embed_files_by_default: bool,
) {
if let Expression::ImageReference(ref mut resource_ref) = e {
match resource_ref {
ImageReference::None => {}
ImageReference::AbsolutePath(path) => {
ImageReference::AbsolutePath(path)
if embed_files_by_default || path.starts_with("builtin:/") =>
{
let mut resources = global_embedded_resources.borrow_mut();
let maybe_id = resources.len();
let resource_id = *resources.entry(path.clone()).or_insert(maybe_id);
Expand All @@ -45,9 +47,11 @@ fn embed_resources_from_expression(
.unwrap_or_default(),
}
}
ImageReference::EmbeddedData { .. } => {}
_ => {}
}
};

e.visit_mut(|mut e| embed_resources_from_expression(&mut e, global_embedded_resources));
e.visit_mut(|mut e| {
embed_resources_from_expression(&mut e, global_embedded_resources, embed_files_by_default)
});
}
19 changes: 1 addition & 18 deletions sixtyfps_compiler/typeloader.rs
Expand Up @@ -389,24 +389,7 @@ impl<'a> TypeLoader<'a> {
match candidate.strip_prefix("builtin:/") {
Err(_) => candidate.exists().then(|| (candidate, None)),
Ok(builtin) => {
let mut components = vec![];
for part in builtin.iter() {
if part == ".." {
components.pop();
} else if part != "." {
components.push(part);
}
}
if let &[folder, file] = components.as_slice() {
let library =
crate::library::widget_library().iter().find(|x| x.0 == folder)?.1;
library
.iter()
.find(|vf| vf.path == file)
.map(|vf| (candidate, Some(vf.contents)))
} else {
None
}
crate::library::load_file(builtin).map(|data| (candidate, Some(data)))
}
}
})
Expand Down
25 changes: 24 additions & 1 deletion sixtyfps_compiler/widgets/common/common.60
Expand Up @@ -84,4 +84,27 @@ export TextEdit := ScrollView {
}
}
}
}
}

export AboutSixtyFPS := Rectangle {
VerticalLayout {
padding: 12px;
spacing: 8px;
alignment: start;
Text {
text: "Made width";
font-size: 24px;
font-weight: 700;
horizontal-alignment: center;
}
Image {
source: @image-url("sixtyfps_logo.svg");
preferred-width: 128px;
}
Text {
text: "Version 0.1.3\n© 2021 SixtyFPS GmbH\nhttps://sixtyfps.io/";
font-size: 10px;
horizontal-alignment: center;
}
}
}
3 changes: 3 additions & 0 deletions sixtyfps_compiler/widgets/common/sixtyfps_logo.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions sixtyfps_compiler/widgets/fluent/sixtyfps_widgets.60
Expand Up @@ -8,10 +8,10 @@
Please contact info@sixtyfps.io for more information.
LICENSE END */

import { LineEditInner, TextEdit } from "../common/common.60";
import { LineEditInner, TextEdit, AboutSixtyFPS } from "../common/common.60";
import { StandardButton } from "../common/standardbutton.60";
import { StyleMetrics, ScrollView, Button, Palette } from "sixtyfps_widgets_impl.60";
export { StyleMetrics, ScrollView, Button, StandardButton, TextEdit }
export { StyleMetrics, ScrollView, Button, StandardButton, TextEdit, AboutSixtyFPS }

export CheckBox := Rectangle {
callback toggled;
Expand Down
4 changes: 2 additions & 2 deletions sixtyfps_compiler/widgets/native/sixtyfps_widgets.60
Expand Up @@ -8,9 +8,9 @@
Please contact info@sixtyfps.io for more information.
LICENSE END */

import { LineEditInner, TextEdit } from "../common/common.60";
import { LineEditInner, TextEdit, AboutSixtyFPS } from "../common/common.60";
import { StyleMetrics, ScrollView } from "sixtyfps_widgets_impl.60";
export { StyleMetrics, ScrollView, TextEdit }
export { StyleMetrics, ScrollView, TextEdit, AboutSixtyFPS }

// FIXME: the font-size should be removed but is required right now to compile the printer-demo
export Button := NativeButton {
Expand Down
4 changes: 2 additions & 2 deletions sixtyfps_compiler/widgets/ugly/sixtyfps_widgets.60
Expand Up @@ -9,10 +9,10 @@
LICENSE END */


import { LineEditInner, TextEdit } from "../common/common.60";
import { LineEditInner, TextEdit, AboutSixtyFPS } from "../common/common.60";
import { StandardButton } from "../common/standardbutton.60";
import { StyleMetrics, ScrollView, Button, Palette } from "sixtyfps_widgets_impl.60";
export { StyleMetrics, ScrollView, Button, StandardButton, TextEdit }
export { StyleMetrics, ScrollView, Button, StandardButton, TextEdit, AboutSixtyFPS }

export CheckBox := Rectangle {
callback toggled;
Expand Down

0 comments on commit 4a26fae

Please sign in to comment.