Skip to content

Instantly share code, notes, and snippets.

View Sergio0694's full-sized avatar
🚀
Coding all day

Sergio Pedri Sergio0694

🚀
Coding all day
View GitHub Profile
@Sergio0694
Sergio0694 / GetAttachedSemiAcrylicEffectAsync.cs
Last active April 22, 2022 17:04
A method that combines Composition APIs and Win2D to replicate the Acrylic brush on Windows 10 Creator's Update
/// <summary>
/// Gets a shared semaphore to avoid loading multiple Win2D resources at the same time
/// </summary>
private static readonly SemaphoreSlim Win2DSemaphore = new SemaphoreSlim(1);
/// <summary>
/// Creates an effect brush that's similar to the official Acrylic brush in the Fall Creator's Update.
/// The pipeline uses the following effects: HostBackdropBrush > <see cref="LuminanceToAlphaEffect"/> >
/// <see cref="OpacityEffect"/> > <see cref="BlendEffect"/> > <see cref="ArithmeticCompositeEffect"/> >
/// <see cref="ColorSourceEffect"/> > <see cref="BorderEffect"/> with customizable blend factors for each couple of layers
@Sergio0694
Sergio0694 / EventAwaiter.cs
Last active November 25, 2020 04:56
A helper to await for events in an asynchronous manner.
/// <summary>
/// A helper to await for events in an asynchronous manner.
/// </summary>
public static class EventAwaiter
{
/// <summary>
/// Awaits a specific event to be raised and executes a callback when that happens.
/// </summary>
/// <typeparam name="THandler">The type of handler to await.</typeparam>
/// <param name="register">An <see cref="Action{T}"/> to register the event.</param>
@Sergio0694
Sergio0694 / BenchmarkRunner.cs
Last active March 19, 2020 10:16
A simple class that can run a benchmark and display its results with a markdown table
using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using System.Text;
#nullable enable
namespace Profiling
{
@Sergio0694
Sergio0694 / ItemClickBehavior.cs
Created January 14, 2020 11:31
A behavior that listens for the ListViewBase.ItemClick event on its source and executes a specified command when that event is fired
using System.Windows.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Microsoft.Xaml.Interactivity;
#nullable enable
namespace Microsoft.Xaml.Interactions.Core
{
/// <summary>
@Sergio0694
Sergio0694 / ReadOnlySpanExtensions.cs
Last active January 10, 2020 15:04
A fast and allocation-free API to tokenize sequences of items
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
namespace System
{
/// <summary>
/// A <see langword="class"/> with some extension methods for the <see cref="ReadOnlySpan{T}"/> type
/// </summary>
public static class ReadOnlySpanExtensions
{
@Sergio0694
Sergio0694 / ReadOnlySpanEnumerator.cs
Last active January 6, 2020 00:19
A fast and allocation-free method to enumerate items in a collection
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace System
{
/// <summary>
/// A <see langword="ref"/> <see langword="struct"/> that enumerates the items in a given <see cref="ReadOnlySpan{T}"/> instance
/// </summary>
/// <typeparam name="T">The type of items to enumerate</typeparam>
@Sergio0694
Sergio0694 / MaxBenchmark.cs
Created December 3, 2019 10:40
A benchmark of various no-branch methods to compute the max of two int values
using System;
using System.Buffers;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace MaxBenchmark
{
class Program
// Custom delegate that takes the two ref parameters we need
public delegate void DataLoader(object instance, ref object r0, ref byte r1);
/* The method that takes a delegate and the list of discovered
* fields for the closure type and its nested closure types,
* and builds our dynamic IL method that loads all the fields sequentially */
public static DataLoader BuildDataLoader(
Delegate instance
IReadOnlyList<ClosureField> fields)
{
public static void Main()
{
int[] array = new int[100];
int value = 1;
Action<int> action = i => array[i] = value;
}
[CompilerGenerated]
private sealed class <>c__DisplayClass0_0
{
public int[] array;
public int value;
internal void <Main>b__0(int i)
{
array[i] = value;