tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

composite_view.rs (4331B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 use webrender_api::ColorF;
      6 use webrender_api::debugger::CompositorDebugInfo;
      7 
      8 pub fn ui(
      9    ui: &mut egui::Ui,
     10    info: &mut CompositorDebugInfo,
     11 ) {
     12    let mut valid_z_layers = 0;
     13    for tile in &info.tiles {
     14        valid_z_layers |= 1 << tile.z_id;
     15    }
     16 
     17    let colors = [
     18        ColorF::new(1.0, 1.0, 1.0, 1.0),
     19        ColorF::new(1.0, 0.0, 0.0, 1.0),
     20        ColorF::new(0.0, 1.0, 0.0, 1.0),
     21        ColorF::new(0.7, 0.7, 1.0, 1.0),
     22        ColorF::new(1.0, 1.0, 0.0, 1.0),
     23        ColorF::new(0.0, 1.0, 1.0, 1.0),
     24        ColorF::new(1.0, 0.0, 1.0, 1.0),
     25    ];
     26 
     27    ui.horizontal(|ui| {
     28        for i in 0..32 {
     29            if valid_z_layers & 1 << i != 0 {
     30                let mut enabled = info.enabled_z_layers & (1 << i) != 0;
     31                if ui.checkbox(&mut enabled, format!("z_id {}", i)).changed() {
     32                    if enabled {
     33                        info.enabled_z_layers |= 1 << i;
     34                    } else {
     35                        info.enabled_z_layers &= !(1 << i);
     36                    }
     37                }
     38            }
     39        }
     40    });
     41 
     42    let (response, painter) = ui.allocate_painter(
     43        ui.available_size(),
     44        egui::Sense::hover(),
     45    );
     46 
     47    let rect = response.rect;
     48 
     49    // Background
     50    painter.rect_filled(rect, 0.0, egui::Color32::from_rgb(77, 77, 77));
     51 
     52    let mut x_max = -f32::INFINITY;
     53    let mut x_min = f32::INFINITY;
     54    let mut y_max = -f32::INFINITY;
     55    let mut y_min = f32::INFINITY;
     56 
     57    for tile in &info.tiles {
     58        x_min = x_min.min(tile.device_rect.min.x);
     59        x_max = x_max.max(tile.device_rect.max.x);
     60        y_min = y_min.min(tile.device_rect.min.y);
     61        y_max = y_max.max(tile.device_rect.max.y);
     62    }
     63 
     64    let src_width = x_max - x_min;
     65    let src_height = y_max - y_min;
     66 
     67    let dest_width = rect.width();
     68    let dest_height = rect.height();
     69 
     70    let scale_x = dest_width / src_width;
     71    let scale_y = dest_height / src_height;
     72    let scale = scale_x.min(scale_y);
     73 
     74    let scaled_width = src_width * scale;
     75    let scaled_height = src_height * scale;
     76 
     77    let offset_x = 0.5 * (dest_width - scaled_width);
     78    let offset_y = 0.5 * (dest_height - scaled_height);
     79 
     80    let sx0 = rect.left() + (x_min - x_min) * scale + offset_x;
     81    let sx1 = rect.left() + (x_max - x_min) * scale + offset_x;
     82    let sy0 = rect.top() + (y_min - y_min) * scale + offset_y;
     83    let sy1 = rect.top() + (y_max - y_min) * scale + offset_y;
     84 
     85    painter.rect_stroke(
     86        egui::Rect::from_min_max(egui::pos2(sx0, sy0), egui::pos2(sx1, sy1)),
     87        0.0,
     88        egui::Stroke::new(1.0, egui::Color32::WHITE),
     89        egui::StrokeKind::Outside,
     90    );
     91 
     92    for tile in &info.tiles {
     93        if info.enabled_z_layers & (1 << tile.z_id) == 0 {
     94            continue;
     95        }
     96 
     97        let rect_opt = tile.device_rect.intersection(&tile.clip_rect);
     98 
     99        if let Some(tile_rect) = rect_opt {
    100            let x0 = rect.left() + (tile_rect.min.x - x_min) * scale + offset_x;
    101            let x1 = rect.left() + (tile_rect.max.x - x_min) * scale + offset_x;
    102            let y0 = rect.top() + (tile_rect.min.y - y_min) * scale + offset_y;
    103            let y1 = rect.top() + (tile_rect.max.y - y_min) * scale + offset_y;
    104 
    105            let color = colors[tile.z_id as usize % colors.len()];
    106            let fill_color = egui::Color32::from_rgba_premultiplied(
    107                (color.r * 255.0 * 0.3) as u8,
    108                (color.g * 255.0 * 0.3) as u8,
    109                (color.b * 255.0 * 0.3) as u8,
    110                (color.a * 255.0 * 0.3) as u8,
    111            );
    112            let stroke_color = egui::Color32::from_rgba_premultiplied(
    113                (color.r * 255.0) as u8,
    114                (color.g * 255.0) as u8,
    115                (color.b * 255.0) as u8,
    116                (color.a * 255.0) as u8,
    117            );
    118 
    119            let tile_rect = egui::Rect::from_min_max(
    120                egui::pos2(x0, y0),
    121                egui::pos2(x1, y1),
    122            );
    123 
    124            painter.rect_filled(tile_rect, 0.0, fill_color);
    125            painter.rect_stroke(tile_rect, 0.0, egui::Stroke::new(1.0, stroke_color), egui::StrokeKind::Outside);
    126        }
    127    }
    128 }