Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul package #45

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
992ac86
ignore system-specific temp, backup, thumbnail etc. files
salim-b Oct 11, 2020
48ccc6b
add @salim-b as contributor
salim-b Oct 11, 2020
aca7027
avoid `$` due to partial matching
salim-b Oct 11, 2020
3fdd156
add lintr config
salim-b Oct 12, 2020
5a166ca
improve pkg title and description
salim-b Oct 12, 2020
e83e1c2
tidy DESCRIPTION
salim-b Oct 12, 2020
756017d
enable roxygen2 markdown support pkg-wide
salim-b Oct 13, 2020
752e311
set explicit line end conversion in project config
salim-b Oct 13, 2020
1234a1c
lintr config: add object length linter
salim-b Oct 13, 2020
9b4467f
add additional dependencies
salim-b Oct 13, 2020
9c639c2
use comments as rscodeio identifiers in custom QSS files
salim-b Oct 13, 2020
8a68484
overhaul package:
salim-b Oct 13, 2020
2608140
remove unnecessary noise
salim-b Oct 13, 2020
869b16a
minor doc change
salim-b Oct 13, 2020
166340c
remove redundancy
salim-b Oct 14, 2020
138a28a
avoid pipe operator
salim-b Oct 14, 2020
e21a6e1
fix styling of hyperlinked <code> in help pane
salim-b Oct 14, 2020
38d3308
document()
salim-b Oct 14, 2020
b5dd995
set per-project Rmd formatting harmonization
salim-b Oct 15, 2020
2ee99f9
update README
salim-b Oct 15, 2020
5e15c2d
be more precise about QSS terminology
salim-b Oct 15, 2020
cb9ebdf
fix formatting of doc
salim-b Oct 16, 2020
964295b
be more precise
salim-b Oct 17, 2020
35892ff
mention necessary menu bar styling reinstallation after RStudio update
salim-b Oct 17, 2020
59303cf
update pkg version (dev)
salim-b Oct 17, 2020
8a9e739
`install_menu_theme()`: add param `reload_editor_theme`
salim-b Oct 23, 2020
24859df
write README in R Markdown format and let knitr/Pandoc create `README…
salim-b Jan 27, 2021
c3db00e
gitignore README source file
salim-b Apr 30, 2022
c00d74d
update RoxygenNote
salim-b Apr 30, 2022
6fc324c
tweak help pane CSS
salim-b Apr 30, 2022
cba1200
Rproj conf: clean package sources prior to installation
salim-b May 12, 2022
1c53a82
fix package installation hint background color
salim-b May 12, 2022
5715ec4
fix history search results background color
salim-b May 12, 2022
29515ea
fix history search results background color (2)
salim-b May 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
^.*\.Rproj$
^\.Rproj\.user$

# other
^LICENSE\.md$
^README\.Rmd$
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,10 @@ vignettes/*.pdf
*.utf8.md
*.knit.md
.Rproj.user

# System-specific temp, backup, thumbnail etc. files
.~lock*
*.backup
~$*
.DS_Store
Thumbs.db
12 changes: 12 additions & 0 deletions .lintr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
linters: with_defaults(absolute_path_linter = absolute_path_linter,
implicit_integer_linter = implicit_integer_linter,
line_length_linter = line_length_linter(160),
nonportable_path_linter = nonportable_path_linter,
object_length_linter = lintr::object_length_linter(40),
semicolon_terminator_linter = semicolon_terminator_linter,
todo_comment_linter = todo_comment_linter,
trailing_whitespace_linter = NULL,
T_and_F_symbol_linter = T_and_F_symbol_linter,
undesirable_function_linter = undesirable_function_linter,
undesirable_operator_linter = undesirable_operator_linter(c(lintr:::default_undesirable_operators, `->` = NA)),
unneeded_concatenation_linter = unneeded_concatenation_linter)
42 changes: 33 additions & 9 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
Package: rscodeio
Title: A VSCode skin for RStudio
Version: 0.1.0
Authors@R: c(person("Anthony", "North", email = "anthony.jl.north@gmail.com", role = c("aut", "cre")),
person("Miles", "McBain", email = "miles.mcbain@gmail.com", role = "ctb"))
Description: It really just modifies RStudio presentation.
Depends: R (>= 3.3.0)
Title: An RStudio Theme Inspired by Visual Studio Code
Version: 0.1.9000
Authors@R:
c(person(given = "Anthony",
family = "North",
role = c("aut", "cre"),
email = "anthony.jl.north@gmail.com"),
person(given = "Miles",
family = "McBain",
role = "ctb",
email = "miles.mcbain@gmail.com"),
person(given = "Salim",
family = "Brüggemann",
role = "ctb",
email = "salim-b@pm.me",
comment = c(ORCID = "0000-0002-5329-5987")))
Description: This package provides two greyish dark RStudio
editor themes which only differ in the syntax highlighting style:
while 'rscodeio' offers the colors found in Visual Studio Code,
'Tomorrow Night Bright (rscodeio)' combines this package's dark
interface theming with the syntax highlighting colors from the
similarly named editor theme provided by RStudio. Furthermore, both
themes style RStudio's menu, something which is not exposed by the
current theming API and therefore requires administrator privileges to
replace the relevant QSS files.
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Depends:
R (>= 3.3.0)
Imports:
fs,
glue,
purrr,
rlang,
rstudioapi
RoxygenNote: 7.1.1
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2
8 changes: 4 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(activate_menu_theme)
export(deactivate_menu_theme)
export(install_theme)
export(uninstall_theme)
export(install_menu_theme)
export(install_themes)
export(uninstall_menu_theme)
export(uninstall_themes)
240 changes: 150 additions & 90 deletions R/rscodeio.R
Original file line number Diff line number Diff line change
@@ -1,120 +1,180 @@
#' Install the rscodeio theme
#' Install rscodeio editor themes and menu bar styling
#'
#' You'll need RStudio at least 1.2.x and if your RStudio
#' is installed to a default location on Windows or Linux,
#' you'll need to be running RStudio as
#' Administrator to install the menu theme files (Just required for install).
#' RStudio 1.2 or later is required for this. Additionally, administrator privileges are required to also install the Qt Style Sheet (QSS) files to style
#' RStudio's menu bar, something which is not possible with [RStudio's current theming
#' API](https://rstudio.github.io/rstudio-extensions/rstudio-theme-creation.html).
#'
#' You can elect not to install the menu theme files with `menus = FALSE`.
#' # Menu bar styling
#'
#' @param menus if FALSE do not install the RStudio menu theme qss files.
#' @return nothing.
#' On Linux, it will be asked interactively for administrator credentials by calling [Polkit](https://en.wikipedia.org/wiki/Polkit)'s
#' [`pkexec`](https://www.freedesktop.org/software/polkit/docs/latest/pkexec.1.html). Windows users must run RStudio as administrator in order to install
#' rscodeio's menu bar styling. On macOS and RStudio Server, menu bar styling is not necessary and thus always skipped.
#'
#' The installation of the QSS files styling the menu bar can be skipped by setting `style_menu_bar = FALSE`. See [install_menu_theme()] for more details about
#' the menu bar styling.
#'
#' Note that the menu bar styling has to be reapplied after _every_ update or reinstallation of RStudio. This is because the custom QSS files provided by
#' rscodeio get overwritten during RStudio's installation process.
#'
#' @param apply_theme The rscodeio editor theme variant to apply. All variants are installed and the one specified here is activated right away. The other
#' variants can be selected anytime later in RStudio's global options. Variation only affects the syntax highlighting and terminal styling. One of
#' - `"rscodeio"` for the default color variant inspired by Visual Studio Code.
#' - `"Tomorrow Night Bright (rscodeio)"` for the colors known from the similarly named default RStudio theme.
#' @param style_menu_bar Whether to also install rscodeio's custom QSS files to style RStudio's menu bar. Administrator privileges are required.
#' @param install_globally Whether to install the editor themes for the current user (`FALSE`) or all users globally (`TRUE`). The latter may require
#' administrator privileges.
#' @return Nothing (`NULL` invisibly).
#' @export
install_theme <- function(menus = TRUE) {

## check RStudio API available
if(!rstudioapi::isAvailable()) stop("RSCodeio must be installed from within RStudio.")
install_themes <- function(apply_theme = c("rscodeio",
"Tomorrow Night Bright (rscodeio)"),
style_menu_bar = TRUE,
install_globally = FALSE) {

# ensure RStudio API is available
if (!rstudioapi::isAvailable()) {
stop("rscodeio must be installed from within RStudio.",
call. = FALSE)
}

## check RStudio supports themes
if(utils::compareVersion(as.character(rstudioapi::versionInfo()$version), "1.2.0") < 0)
stop("You need RStudio 1.2 or greater to get theme support")
# ensure minimally required RStudio version
if (rstudioapi::versionInfo()$version < as.package_version("1.2.0"))
stop("You need RStudio 1.2 or later to get theme support",
call. = FALSE)

## check if menu theme already installed and uninstall
if(rscodeio_installed()){
uninstall_theme()
}
# add the editor theme variants
## existing editor themes of the same names have to be removed first
uninstall_themes(restore_menu_bar = FALSE)

## add the themes
rscodeio_default_theme <- rstudioapi::addTheme(fs::path_package(package = "rscodeio",
"resources","rscodeio.rstheme"))

rstudioapi::addTheme(fs::path_package(package = "rscodeio",
"resources","rscodeio_tomorrow_night_bright.rstheme"))
purrr::walk(.x = c("rscodeio.rstheme",
"rscodeio_tomorrow_night_bright.rstheme"),
.f = ~ rstudioapi::addTheme(fs::path_package(package = "rscodeio",
"resources", .x),
globally = install_globally))

## add the custom Qt CSS
if (menus) activate_menu_theme()
# add the custom QSS files
if (style_menu_bar) install_menu_theme(reload_editor_theme = FALSE)

## activate default rscodeio theme
rstudioapi::applyTheme(rscodeio_default_theme)
# activate chosen rscodeio editor theme variant
rstudioapi::applyTheme(rlang::arg_match(apply_theme))
}

#' Uninstall the rscodeio theme
#' Uninstall the rscodeio editor themes and menu bar styling
#'
#' To restore RStudio's default _dark_ Qt Style Sheet (QSS) files (i.e. setting `restore_menu_bar = TRUE`), administrator privileges are required. On Linux, it
#' will be asked interactively for administrator credentials by calling [Polkit](https://en.wikipedia.org/wiki/Polkit)'s
#' [`pkexec`](https://www.freedesktop.org/software/polkit/docs/latest/pkexec.1.html). Windows users must run RStudio as administrator in order to uninstall
#' rscodeio's menu bar styling. On macOS and RStudio Server, menu bar styling is not necessary and thus its menu bar restoration will be skipped.
#'
#' @return nothing.
#' @param restore_menu_bar Whether to restore RStudio's default menu bar styling. Irrelevant if rscodeio menu bar styling is not installed (see
#' [install_menu_theme()] for details about menu bar styling).
#' @return Nothing (`NULL` invisibly).
#' @export
uninstall_theme <- function(){

deactivate_menu_theme()

installed_rscodeio_themes <- grep(x = purrr::map_depth(.x = rstudioapi::getThemes(),
.depth = 1L,
.f = purrr::pluck("name")),
pattern = "rscodeio",
value = TRUE)

for (theme in installed_rscodeio_themes) {
rstudioapi::removeTheme(theme)
}
}
uninstall_themes <- function(restore_menu_bar = TRUE) {

if (restore_menu_bar) uninstall_menu_theme()

purrr::walk(.x = installed_rscodeio_editor_themes(),
.f = rstudioapi::removeTheme)
}

#' Activate rscodeio styling in file menu.
#' Install rscodeio menu bar styling
#'
#' This function overwrites RStudio's default _dark_ [Qt Style Sheet (QSS)](https://doc.qt.io/Qt-5/stylesheet-syntax.html) files with rscodeio's customized ones
#' in order to properly style RStudio's menu bar which cannot be altered by [RStudio's current theming
#' API](https://rstudio.github.io/rstudio-extensions/rstudio-theme-creation.html). The original Qt Style Sheet files are backed up and can be restored by
#' calling [uninstall_menu_theme()].
#'
#' Running [`install_themes(style_menu_bar = TRUE)`][install_themes()] (which is the default) has the same effect as running this function.
#' `install_menu_theme()` can be useful to install the rscodeio menu bar styling _without_ the accompanying editor themes.
#'
#' @return nothing.
#' Administrator privileges are required to copy the QSS files to RStudio's installation directory. On Linux, it will be asked interactively for administrator
#' credentials by calling [Polkit](https://en.wikipedia.org/wiki/Polkit)'s [`pkexec`](https://www.freedesktop.org/software/polkit/docs/latest/pkexec.1.html).
#' Windows users must run RStudio as administrator On macOS and RStudio Server, menu bar styling is not necessary and running this function has no effect.
#'
#' Note that this function has to be executed again after _every_ update or reinstallation of RStudio. This is because the custom QSS files provided by rscodeio
#' get overwritten during RStudio's installation process.
#'
#' @param reload_editor_theme Whether to reload the editor theme upon installing rscodeio's menu bar styling. This is necessary for the changes to take effect
#' immediately. Otherwise, the changes will only become visible after a restart of RStudio.
#' @return Nothing (`NULL` invisibly).
#' @export
activate_menu_theme <- function() {
install_menu_theme <- function(reload_editor_theme = TRUE) {

## Styling menus not supported on Mac or RStudio Server.
if(host_os_is_mac() | is_rstudio_server()) return(NULL)
# menu bar styling is not supported on macOS or RStudio Server
if (is_macos() || is_rstudio_server()) return(NULL)

if(file.exists(gnome_theme_dark_backup()) |
file.exists(windows_theme_dark_backup())) {
message("RSCodeio menu theme already activated. Deactivate first.")
return(FALSE)
}
process_menu_themes(backup_gnome = !is_rscodeio_menu_theme(path_theme_dark_gnome()),
backup_windows = !is_rscodeio_menu_theme(path_theme_dark_windows()),
override_gnome = TRUE,
override_windows = TRUE)

## backup dark Qt themes
file.copy(from = gnome_theme_dark(),
to = gnome_theme_dark_backup())
file.copy(from = windows_theme_dark(),
to = windows_theme_dark_backup())

## replace with RSCodeio Qt themes
file.copy(from = system.file(fs::path("resources","stylesheets","rstudio-gnome-dark.qss"),
package = "rscodeio"),
to = gnome_theme_dark(),
overwrite = TRUE)
file.copy(from = system.file(fs::path("resources","stylesheets","rstudio-windows-dark.qss"),
package = "rscodeio"),
to = windows_theme_dark(),
overwrite = TRUE)
if (reload_editor_theme) rstudioapi::applyTheme(rstudioapi::getThemeInfo()$editor)
}

#' Deactivate rscodeio style in file menu.
#' Uninstall rscodeio menu bar styling
#'
#' This function restores RStudio's default _dark_ Qt [Qt Style Sheet (QSS)](https://doc.qt.io/Qt-5/stylesheet-syntax.html) files.
#'
#' @return nothing.
#' Running [`uninstall_themes(restore_menu_bar = TRUE)`][uninstall_themes()] (which is the default) has the same effect as running this function.
#' `uninstall_menu_theme()` can be useful to remove the rscodeio menu bar styling _without_ removing the accompanying editor themes.
#'
#' Administrator privileges are required to restore RStudio's default QSS files. On Linux, it will be asked interactively for administrator credentials by
#' calling [Polkit](https://en.wikipedia.org/wiki/Polkit)'s [`pkexec`](https://www.freedesktop.org/software/polkit/docs/latest/pkexec.1.html). Windows users
#' must run RStudio as administrator. On macOS and RStudio Server, menu bar styling is not necessary and running this function has no effect.
#'
#' @return Nothing (`NULL` invisibly).
#' @export
deactivate_menu_theme <- function(){

## Styling menus not supported on Mac.
if(host_os_is_mac()) return(NULL)
uninstall_menu_theme <- function() {

## menu bar styling not supported on Mac
if (is_macos()) return(NULL)

path_theme_dark_gnome <- path_theme_dark_gnome()
path_theme_dark_gnome_backup <- path_theme_dark_gnome_backup()
path_theme_dark_windows <- path_theme_dark_windows()
path_theme_dark_windows_backup <- path_theme_dark_windows_backup()
is_rscodeio_current_gnome <- is_rscodeio_menu_theme(path_theme_dark_gnome)
is_rscodeio_current_windows <- is_rscodeio_menu_theme(path_theme_dark_windows)
exists_theme_dark_gnome_backup <- fs::file_exists(path_theme_dark_gnome_backup)
exists_theme_dark_windows_backup <- fs::file_exists(path_theme_dark_windows_backup)

# shouldn't really happen
if (exists_theme_dark_gnome_backup && is_rscodeio_menu_theme(path_theme_dark_gnome_backup)) {

stop("The following backed up file supposed to be an unmodified RStudio QSS file is actually an rscodeio QSS file:\n", path_theme_dark_gnome_backup,
"\n", "Delete the file if you'd like this error to be gone.\nUsing the R console: fs::file_delete(rscodeio:::path_theme_dark_gnome_backup())",
call. = FALSE)
}
if (exists_theme_dark_windows_backup && is_rscodeio_menu_theme(path_theme_dark_windows_backup)) {

if(!file.exists(gnome_theme_dark_backup()) |
!file.exists(windows_theme_dark_backup())) {
message("RStudio theme backups not found. rscodeio already deactivated?")
return(FALSE)
stop("The following backed up file supposed to be an unmodified RStudio QSS file is actually an rscodeio QSS file:\n", path_theme_dark_windows_backup, "\n",
"Delete the file if you'd like this error to be gone.\nUsing the R console: fs::file_delete(rscodeio:::path_theme_dark_windows_backup())",
call. = FALSE)
}

## restore dark Qt themes
file.copy(from = gnome_theme_dark_backup(),
to = gnome_theme_dark(),
overwrite = TRUE)
file.copy(from = windows_theme_dark_backup(),
to = windows_theme_dark(),
overwrite = TRUE)
process_menu_themes(restore_gnome = exists_theme_dark_gnome_backup && is_rscodeio_current_gnome,
restore_windows = exists_theme_dark_windows_backup && is_rscodeio_current_windows)

# print instructive warnings if restoral is impossible
if (!exists_theme_dark_gnome_backup && is_rscodeio_current_gnome) {

warning("Unable to restore default RStudio menu bar styling for GNOME because the corresponding backup file couldn't be found.\n",
"The most convenient way to restore the original file at ", path_theme_dark_gnome, " is to simply update or reinstall RStudio.\n",
"RStudio stable release installers are available at https://rstudio.com/products/rstudio/download/",
call. = FALSE)
}
if (!exists_theme_dark_windows_backup && is_rscodeio_current_windows) {

## delete backups
unlink(gnome_theme_dark_backup())
unlink(windows_theme_dark_backup())
warning("Unable to restore default RStudio menu bar styling for Windows because the corresponding backup file couldn't be found.\n",
"The most convenient way to restore the original file at ", path_theme_dark_windows, " is to simply update or reinstall RStudio.\n",
"RStudio stable release installers are available at https://rstudio.com/products/rstudio/download/",
call. = FALSE)
}

# display message if rscodeio's menu bar styling has already been removed
if (!exists_theme_dark_gnome_backup && !exists_theme_dark_windows_backup) {
if (!is_rscodeio_current_gnome && !is_rscodeio_current_windows) {
message("rscodeio's menu bar styling is already removed.")
}
}
}
Loading