Skip to content

Commit

Permalink
1.3.0 - Trying to set stylesheet on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
Paulo Galardi committed Sep 28, 2017
1 parent c1673b1 commit 2c55ced
Show file tree
Hide file tree
Showing 4 changed files with 341 additions and 9 deletions.
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ executable(
'src/Widgets/WebView.vala',
'src/Widgets/Preferences.vala',
'src/Widgets/Cheatsheet.vala',
'src/Styles/quilter.vala',
'src/Styles/quilterdark.vala',
'src/Constants/AppSettings.vala',
'src/Services/FileUtils.vala',
'src/Services/DialogUtils.vala',
Expand Down
153 changes: 153 additions & 0 deletions src/Styles/quilter.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
The MIT License (MIT)
Copyright (c) 2014-2015 John Otander
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

public class Quilter.Styles.quilter {
public const string css = """
html {
font-size: 16px;
}
body {
line-height: 1.5;
}
p,
.air-p {
font-size: 1rem;
}
h1,
.air-h1,
h2,
.air-h2,
h3,
.air-h3,
h4,
.air-h4 {
margin: 1.214rem 0 .5rem;
font-weight: inherit;
}
h1,
.air-h1 {
margin-top: 0;
font-size: 3rem;
}
h2,
.air-h2 {
font-size: 2rem;
}
h3,
.air-h3 {
font-size: 1.5rem;
}
h4,
.air-h4 {
font-size: 1.214rem;
}
h5,
.air-h5 {
font-size: 1.121rem;
}
h6,
.air-h6 {
font-size: .88rem;
}
small,
.air-small {
font-size: .707em;
}
/* https://github.com/mrmrs/fluidity */
img,
canvas,
iframe,
video,
svg,
select,
textarea {
display: block;
max-width: 40%;
}
body {
color: #222;
font-family: 'Open Sans', Helvetica, sans-serif;
font-weight: 400;
margin-left: 80px;
margin-right: 80px;
margin-top: 40px;
max-width: 48rem;
text-align: left;
}
img {
border-radius: 10%;
height: auto;
margin: 0 auto;
}
a,
a:visited {
color: #3498db;
}
a:hover,
a:focus,
a:active {
color: #2980b9;
}
pre {
font-family: 'PT Mono', monospace;
background-color: #fafafa;
padding: 1rem;
text-align: left;
}
blockquote {
margin: 0;
border-left: 5px solid #7a7a7a;
font-style: italic;
padding-left: .8rem;
text-align: left;
}
ul,
ol,
li {
text-align: left;
}
p {
color: #444;
}""";
}
154 changes: 154 additions & 0 deletions src/Styles/quilterdark.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
The MIT License (MIT)
Copyright (c) 2014-2015 John Otander
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

public class Quilter.Styles.quilterdark {
public const string css = """
html {
font-size: 16px;
}
body {
line-height: 1.5;
}
p,
.air-p {
font-size: 1rem;
}
h1,
.air-h1,
h2,
.air-h2,
h3,
.air-h3,
h4,
.air-h4 {
margin: 1.214rem 0 .5rem;
font-weight: inherit;
}
h1,
.air-h1 {
margin-top: 0;
font-size: 3rem;
}
h2,
.air-h2 {
font-size: 2rem;
}
h3,
.air-h3 {
font-size: 1.5rem;
}
h4,
.air-h4 {
font-size: 1.214rem;
}
h5,
.air-h5 {
font-size: 1.121rem;
}
h6,
.air-h6 {
font-size: .88rem;
}
small,
.air-small {
font-size: .707em;
}
/* https://github.com/mrmrs/fluidity */
img,
canvas,
iframe,
video,
svg,
select,
textarea {
display: block;
max-width: 40%;
}
body {
color: #fff;
background-color: #333;
font-family: 'Open Sans', Helvetica, sans-serif;
font-weight: 400;
margin-left: 80px;
margin-right: 80px;
margin-top: 40px;
max-width: 48rem;
text-align: left;
}
img {
border-radius: 10%;
height: auto;
margin: 0 auto;
}
a,
a:visited {
color: #3498db;
}
a:hover,
a:focus,
a:active {
color: #2980b9;
}
pre {
font-family: 'PT Mono', monospace;
background-color: #fff;
padding: 1rem;
text-align: left;
}
blockquote {
margin: 0;
border-left: 5px solid #bbb;
font-style: italic;
padding-left: .8rem;
text-align: left;
}
ul,
ol,
li {
text-align: left;
}
p {
color: #eee;
}""";
}
41 changes: 32 additions & 9 deletions src/Widgets/WebView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ namespace Quilter {
visible = true;
vexpand = true;
hexpand = true;
var settings = get_settings();
settings.enable_plugins = false;
settings.enable_page_cache = false;
var settingsweb = get_settings();
settingsweb.enable_plugins = false;
settingsweb.enable_page_cache = false;
web_context.set_cache_model(WebKit.CacheModel.DOCUMENT_VIEWER);

refresh_page ();
var settings = AppSettings.get_default ();
settings.changed.connect (refresh_page);
}

protected override bool context_menu (
Expand All @@ -42,6 +46,21 @@ namespace Quilter {
return true;
}

private string set_stylesheet () {
var settings = AppSettings.get_default ();
if (!settings.dark_mode) {
string normal = Styles.quilter.css;
return normal;
} else {
string dark = Styles.quilterdark.css;
return dark;
}
}

public void refresh_page () {
set_stylesheet ();
}

/**
* Process the frontmatter of a markdown document, if it exists.
* Returns the frontmatter data and strips the frontmatter from the markdown doc.
Expand Down Expand Up @@ -111,16 +130,20 @@ namespace Quilter {
}

public void update_html_view () {
var settings = AppSettings.get_default ();
var file = File.new_for_path (settings.last_file);
string text;
//var settings = AppSettings.get_default ();
//var file = File.new_for_path (settings.last_file);
//string text;

string filename = file.get_path ();
GLib.FileUtils.get_contents (filename, out text);
//string filename = file.get_path ();
//GLib.FileUtils.get_contents (filename, out text);

string text = Widgets.SourceView.buffer.text;

string html = "";
html += "<html><head>";
//html += "<style>"+render_stylesheet+"</style>";
html += "<style>";
html += set_stylesheet ();
html += "</style>";
html += "</head><body><div class=\"markdown-body\">";
html += process (text);
html += "</div></body></html>";
Expand Down

0 comments on commit 2c55ced

Please sign in to comment.